passing multiple parameters in @html.actionlink()

2019-08-08 23:41发布

问题:

I'm using actionlink to pass parameter "code" to controller for an item and get details for that item on new view (actually, it's controller that calls 3 partial views)

I also need to pass parameter "description" but that's just an value that need to be shown as on new view and has no use in controller (i'm using "code" to filter database records).

@foreach (var item in Model.MyModel)
{
  <tr>
    <td>
      @Html.ActionLink(item.code, "Index", "ControlerName", new { pos = item.code.ToString(), desc = item.desc.ToString() }, null)
</td>

}

this actionlink give me url /ControlerName?pos=code&desc=desc

Is there a way to send "desc" parameter so it won't be seen in url but only shown as in new view?

回答1:

No, Html.ActionLink only exists to generate a url and plop it inside an anchor tag. You should be repopulating the desc value from your Index action when it's hit.



回答2:

You have to pass the parameter like you have it so that the other controller and view is aware of the data that's being persisted.

An alternative would be to store your description in a "lookup table" that you can identify by id, so you only need to pass the id and it will be the Controller's or ViewModel's responsibility to look that up.

@Html.ActionLink(item.code, "Index", "ControllerName", new { id=1 }, null)

Controller:

public ActionResult Index(int id) {
    // Go to DB and get the description by id.
}


回答3:

Use form tag and pass all parameters you want to the actionresult and return any other actionresult(which will carry your desired url with it) you want.

razor asks for ; when doing using(Html.BeginForm())