johndoe
johndoe

Reputation: 195

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

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) 
{
}

Upvotes: 7

Views: 1852

Answers (3)

Levi
Levi

Reputation: 32828

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)

Upvotes: 15

Bill Yang
Bill Yang

Reputation: 1413

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.

Upvotes: 0

Vishal
Vishal

Reputation: 12369

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

Upvotes: -2

Related Questions