0

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
}
3 rules
  • 1,359
  • 3
  • 26
  • 54
  • @StephenMuecke I have tried it before the same but getting the value null but now it's coming amazing magic don't know what happen but it's working thanks – 3 rules Dec 03 '16 at 06:54

1 Answers1

1

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)
  • As per my knowledge I think I should not write id = item.Id right I can sent direct variable na! May be I could wrong. – 3 rules Dec 03 '16 at 06:55
  • You current code will not add a route value so its creating just `../ControllerName./MyActionMethod`, not `../ControllerName./MyActionMethod/someValue` –  Dec 03 '16 at 07:02
  • Note I rejected you edit - The `DefaultModelBinder` is not case sensitive - `new { id = item.Id }` or `new { Id = item.Id }` or `new { iD = item.Id }` or `new { ID = item.Id }` are all identical and will correctly bind. –  Dec 03 '16 at 07:04
  • ohhh ok I see, I thought that by sending id by default it will take as a ../ControllerName./MyActionMethod/someValue but it's wrong anyway I got your point sir and also the edit rejection point as well. – 3 rules Dec 03 '16 at 07:05
  • To explain the first point - the 4th parameter is `object` and the method generates a route value or query string value for each property of the object. In your case your object is `int` and `int` has no properties so nothing is generated. If you were to change it to `string id` and set the 4th parameter as (say) "padhiyar" it would generate `...?length=8` because `string` has a property named `length` and there are 8 characters in "padhiyar" –  Dec 03 '16 at 07:09
  • it's amazing explanation sir now I understand only string and those types who has properties will generate the 4th parameter not int ohhhh ok I see. – 3 rules Dec 03 '16 at 08:13