MVC Html.ActionLink() fails weirdly with routeValues parameter in Asp.Net

It seems simple. In an MVC view page you have an @Html.ActionLink with parameters for action, controller & route values:

@Html.ActionLink("Some text","Action","Controller", new {id=1})

but instead of rendering the text with a link to the action, it renders a link with some weird URL that isn’t what you want at all.

If you use intellisense to look at the overloads for ActionLink, you’ll see that the overload you’ve got is interpreting new {id=1} as html attributes not as route values. You need the overload with one more parameter, which can be null:

@Html.ActionLink("Some text","Action","Controller", new {id=1}, null)

And then it works.

13 thoughts on “MVC Html.ActionLink() fails weirdly with routeValues parameter in Asp.Net

  1. madhukrishna

    Hi sir,

    I wanna display parameters in url as below mentioned when I submit the form.
    test.com/Cal/sum/12/13

    but now it is displaying as test.com/cal/sum?a=12&b=13.
    please help me out

    Reply

Leave a Reply to Chris Cancel reply

Your email address will not be published. Required fields are marked *