R K
R K

Reputation: 133

HTML `id` attribute conflicting with ASP.NET MVC `id` convention

Can someone please help me understand why this following code:

<div>
    @Html.ActionLink("Back", "Index", new {id="backbtn"})
</div>

inside a View is rendering following HTML in the browser:

<a href="/Products/Index/backbtn">Back</a>

instead of following desired HTML:

<a id="backbtn" href="/Products/Index">Back</a>

Thanks in advance

Upvotes: 1

Views: 148

Answers (3)

Juan Salvador Portugal
Juan Salvador Portugal

Reputation: 1319

You are using the wrong overload of the Html.ActionLink method.

You are using this overload ActionLink(HtmlHelper, String, String, Object), the third parameter are the routevalues, no htmlAttributes.

You should use the overload ActionLink(HtmlHelper, String, String, Object, Object), here, the third parameter are routevalues, but the four are htmlAttributes

So you just need to pass null to third parameter

@Html.ActionLink("Back", "Index", null, new {id="backbtn"})

Upvotes: 1

ADyson
ADyson

Reputation: 61859

It's because the 3rd parameter of the method, the one you're providing the id to, is the one used to define route parameters , not HTML attributes. Use the 4th parameter instead, and leave the 3rd one as null:

@Html.ActionLink("Back", "Index", null, new {id="backbtn"})

See the documentation for details of what each parameter of the method is used for.

Upvotes: 1

errorau
errorau

Reputation: 2334

it's worked for me more detail Pass parameter to controller from @Html.ActionLink MVC 4

@Html.ActionLink("Test Link", "SomeAction", "SomeController",null, new {id = "someID" })

Upvotes: 1

Related Questions