[英]Passing a user-typed input from View to Action Controller via combined action link and input in ASP.Net MVC 5?
这是一个ASP.Net MVC 5
项目。
我已阅读以下有用的文章:
解释了如何在ASP.Net MVC
中将数据从View
传递到Controller
。 帖子中的主要思想是
View
中的matching-model form-post(输入)和Action Controller
name-convention-parameters,或 route values
与Action Controller
的参数匹配。 现在,我在这里有一个略有不同的情况...,并希望获得一些解决方法的想法。
与上面的方法(2)相似,我已经有一个带有匿名对象的动作链接,该对象具有使用剃刀动态生成的名称会议参数,如下所示:
@Html.ActionLink("Detail", item.DetailsName, item.ControllerName,
new {
pid = item.Pid, eid = item.Eid, mid = item.Mid, cid = item.Cid,
manNo = item.TypeItem == "Create" ? item.ManCreateNo : item.ManEditNo,
caneNo = item.TypeItem == "Create" ? item.CaneCreateNo : item.CaneEditNo,
command = userCanAccept ? "acc" : "pass", from = ViewBag.From
}, null)
并且由ActionLink
调用的(可能的一种)对应的Action Controller
如下所示:
public ActionResult DetailsChange(int pid, int eid, int mid, int cid, int manNo = 1, int caneNo = 1, string command = null, string from = null, string comment = null)
现在,你可以看到,在Action Link
的参数的View
和Action Controller
“中的S参数Controller
完美匹配, 除了一: comment
。
所有其他参数都可以在呈现时由“按剃刀View
呈现,但是comment
应该是用户输入的输入(文本)。
现在,当按下“ Action Link
时,如何将comment
从View
传递到Controller
以及其余参数? 在ASP.Net MVC 5
是否可以解决此问题?
通常,您将使用表单并将此数据发回,而不使用ActionLink。 但是,由于ActionLink基本上是<a>
,其HREF设置为操作方法URL,因此可以使用javascript附加它(例如,在HTML属性中,如果提供ID为CmtLink,则):
$("#CmtLink").on("click", function() {
var href = $(this).attr("href");
//I assume it doesn't exist, but it's good to check for an existing comment parameter
//also assume at least one parameter present
//Also assume comment control is textbox, not textarea
href += "&comment=" + $("#comment").val();
$("#CmtLink").attr("href", href);
});
单击时,这可以始终获取输入的最新值,或者如果注释为空,则可以取消默认行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.