Friday, June 29, 2012

ASP.NET Html.ActionLink Multiple Parameters Not Working

I ran into this issue in ASP.NET MVC 3 (Razor), I believe this ActionLink call should work fine, but it does not. It returns an error because the routeValues aren’t being passed properly. With only one parameter, the issue didn’t occur.

@Html.ActionLink("Delete", "DeleteSomething", "Profile", new { id = ID, something = @item.Name })

The error message: “The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult OrderDevice(Int32)' in 'MvcKVteam.Controllers.ImportXMLController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.”

The solution was to add “null” for the final variable HtmlAttributes.

@Html.ActionLink("Delete", "DeleteSomething", "Profile", new { id = ID, something = @item.Name }, null)

Whether it’s a bug or by design, it sure isn’t intuitive.