passing multiple parameters in @html.actionlink()

蹲街弑〆低调 提交于 2020-01-06 04:03:42

问题


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



来源:https://stackoverflow.com/questions/21890766/passing-multiple-parameters-in-html-actionlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!