简体   繁体   中英

Passing a user-typed input from View to Action Controller via combined action link and input in ASP.Net MVC 5?

This is an ASP.Net MVC 5 project.

I have read the following useful post:

ASP.Net MVC How to pass data from view to controller

Which explains how to pass data from View to Controller in ASP.Net MVC . The main ideas in the post are either

  1. using matching-model form-post (inputs) in the View and name-convention-parameters in Action Controller , or
  2. make an action-link where we have an anonymous object having name-convention-parameters as route values match with the parameters in the Action Controller .

Now I have a slightly different case here... and would like to get some ideas for work-around.

Similar to method (2) above, I already have an action-link with anonymous object having name-convention parameters generated dynamically using razor like the following:

@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)

And (one of the possible) corresponding Action Controller called by the ActionLink looks like the following:

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)

Now, as you can see, the Action Link 's parameters in the View and the Action Controller 's parameters in the Controller match perfectly, except for one: the comment .

All the other parameters can be rendered by the View by razor at rendering time, but the comment is supposed to be a user-typed input (text).

Now, how do I pass this comment from the View to the Controller , together with the rests of the parameters, when the Action Link is pressed? Is there work around for this in ASP.Net MVC 5 ?

Generally you would use a form and post this data back, not use an ActionLink. But, since the ActionLink is basically an <a> with the HREF set to the action method URL, you can append it using javascript (in HTML attributes, if you give an ID of CmtLink, for example):

$("#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);
});

On click, this can then always grab the most recent value entered, or could cancel the default behavior if comments are empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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