Search code examples
c#asp.net-mvcroutescontrolleractionlink

Asp.net MVC action link


I am writing my first MVC app. While creating link for opening details view, I have to pass id.

In Controller method like as below:

public ActionResult getDetails(int UserMasetrId) 
{
    ...Some Code 
}

In VIEW link was generated as below:

<a title="View Details" href="@Url.Action("getDetails", "UserMaster", new {id=item.UserMasterId})"></a>

For above code link is generating as ...controllername/getDetails/13616. But it throws error:

"The parameters dictionary contains a null entry for parameter 'UserMasterId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult getDetails(Int32)' in 'APP.Controllers.UserMasterController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."

Now if I change action link as below?

<a title="View Details" href="@Url.Action("getDetails", "UserMaster", new {UserMasterId=item.UserMasterId})"></a>

It works fine but link change to ...controllername/getDetails?UserMasterId=13616

So please suggest any solution for parameter name as I write in action method and i want format of url not to show parameter name means the format of url should conrtoller/actionmethod/parametervalue.


Solution

  • from all suggestions and after implementing these suggestions i came to decision that in link and in action method parameter name if you use same parameter name as suggested in default router, the URL will generate as
    ControllerName/actionMthod/value
    and in all other case it will generate
    ControllerName/actionMthod?ParamerName=value.