简体   繁体   中英

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 .

The parameter name in your function should match the query parameter in your button link.

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

You should also declare a Route if you're not using the default Id parameter.

[Route("ControllerName/getDetails/{UserMasterId}")]
public ActionResult getDetails(int? UserMasterId) 
{
   if (id == null)
   {
      return NotFound();
   }
}

What you are using is not ActionLink . You are generating a link for you href action only. You can generate a proper a tag using the HtmlHelper: Html.ActionLink like this:

Html.ActionLink("", "getDetails", "UserMaster", new { item.UserMasterId }, null)

This will generate anchor tag like this assuming item.UserMasterId is 1:

<a href="/UserMaster/getDetails/1"></a> 

And change your Controller method to:

public ActionResult getDetails(int? id) 
{
    ...Some Code 
}

If you just started to learn MVC, it is better for you to learn it right way. You should use Route parameters, not QueryString parameters. So leave your link as it is

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

but fix the action, your action is not an MVC style, should be

[Route("{userMasterId?}")]
public ActionResult getDetails(int? userMasterId) 
{
    ...Some Code 
}

but name doesnt matter, important that route parameter and action parameter names should be the same, you can use this as well

[Route("{id?}")]
public ActionResult getDetails(int? id) 
{
    ...Some Code 
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM