Search code examples
c#asp.net-mvcasp.net-mvc-4actionlinkactionmethod

Get null value in parameter in actionresult from actionlink click on different controller actionmethod


I am using Visual Studio 2015 with C# language and MVC version 4.

I am calling the actionmethod on different Controller on actionlink click event.

@Html.ActionLink(item.ListingDate.ToString("MM/dd/yyyy"), "MyActionMethod", "ControllerName", item.Id , null)

it calls the actionmethod correctly but I am getting null as a value in ActionMethod Id:

public async Task<ActionResult> MyActionMethod(string Id) // it is coming null here
{
      //Mycode
}

Solution

  • The name of your parameter is Id so you need to create an object with that name

    @Html.ActionLink(
        item.ListingDate.ToString("MM/dd/yyyy"),
        "MyActionMethod",
        "ControllerName", 
        new { id = item.Id }, // change this
        null)