How to change action parameters and get it to work without changing routing in asp.net mvc?

試著忘記壹切 提交于 2019-12-07 05:00:39

问题


In my route I have something like this:

controller/action/{id} 

To my knowledge this means it will call any action with the parameter id like the following:

public ActionResult Detail(string id) 
{
}

What do I have to do to make the following work without registering the particular route in global.asax file:

public ActionResult Detail(string customerId) 
{
}

回答1:


have a route like - controller/action/{customerId} or just rename the parameter customerId to id and then use it the particular way you want.




回答2:


If you really don't want to rename the method parameter, you can use BindAttribute to tell MVC what its logical name should be:

public ActionResult Detail([Bind(Prefix = "id")] string customerId)



回答3:


You can also pass customerId as query string, which is normally what I do:

<%: Html.ActionLink("Detail", "Detail", new { @customerId = Model.CustomerID}, null)%>

MVC does not enforce the routing, but rather try to resolve routing based on your url AND query string.



来源:https://stackoverflow.com/questions/4749499/how-to-change-action-parameters-and-get-it-to-work-without-changing-routing-in-a

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