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.

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

    1. I actually found your blog while trying to find the name of this pizza place. In 2009 I was told about this pizza place from a tour guide and thought it was incredible. I’m trying to tell my Dad the name as he’ll be in Rome soon, and I cannot figure it out. I looked at my photos and could tell from the “pizza” sign that it’s the same place. Glad to know I’m not the only one who can’t figure it out.

  1. Hi sir,

    I wanna display parameters in url as below mentioned when I submit the form.

    but now it is displaying as
    please help me out

Comments are closed.

MVC Html.ActionLink() fails weirdly with…

by Chris F Carroll read it in 1 min