繁体   English   中英

通过组合的动作链接和ASP.Net MVC 5中的输入将用户输入的输入从View传递到Action Controller?

[英]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如何将数据从视图传递到控制器

解释了如何在ASP.Net MVC中将数据从View传递到Controller 帖子中的主要思想是

  1. 使用View中的matching-model form-post(输入)和Action Controller name-convention-parameters,或
  2. 创建一个动作链接,其中我们有一个匿名对象,该对象具有名称约定参数,因为route valuesAction 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的参数的ViewAction Controller “中的S参数Controller完美匹配, 除了一: comment

所有其他参数都可以在呈现时由“按剃刀View呈现,但是comment应该是用户输入的输入(文本)。

现在,当按下“ Action Link时,如何将commentView传递到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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM