Search code examples
htmlasp.net-mvchtml-helperhtml.actionlink

Why is MVC actionlink not rendering correctly?


I am passing this :

<%: Html.ActionLink("Edit", "EditCRMRequest", "CRM", new { Id = item.Id })%>

and I am getting in browser :

http://something.com/CRM/EditCRMRequest?Length=3

with Error Code :

The parameters dictionary contains a null entry for parameter 'Id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult EditCRMRequest(Int32)' in 'ApricaCRMEvent.Controllers.CRM.CRMController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

If I explicitly write this in browser it works fine:

http://something.com/CRM/EditCRMRequest?Id=3

Solution

  • Correct way:

    <%: Html.ActionLink("Edit", "EditCRMRequest", "CRM", new { Id = item.Id },null)%>
    

    No overload method like this:

    Html.ActionLink(string text, string action, string controller, object routeValues)
    

    If you write like above, Lenght=3 is represent "CRM". Controller name behave as routeValues

    Correct method is:

    Html.ActionLink(string text, string action, string controller, object routeValues, object htmlAttributes)