Thiago
Thiago

Reputation: 1555

html "id" in actionLink razor helper

I want to specify the html id in ActionLink, but I can't do this:

@html.ActionLink("Controller", "Action", new {@id = "tec"})

because the @id means that tec is a value of id parameter.

On the other hand, if I do

@html.ActionLink("Controller", "Action", new {@class = "tec"})

the result will be:

<a href="Controller/Action" class="tec"></a>

Do you know a way to specify the html id?

I wanna this result:

<a href="Controller/Action" id="tec"></a>

Upvotes: 3

Views: 8470

Answers (2)

Iridio
Iridio

Reputation: 9271

You have to specifiy also the controller and you can remove the @ before id

@Html.ActionLink("mylink", "Action", "Controller", new {id = "tec"})

That's because the signature you are using is not the one relative to HtmlAttributes, but to the routing values. If you do not want to specify the controller, use this

@Html.ActionLink("mylink", "Action", null, new {id = "tec"})

Upvotes: 9

Hari Gillala
Hari Gillala

Reputation: 11916

The method signature in your case should be of this..

   public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes
)

Provide the parameters in the same format. htmlattributes should be for creating new html object.

Upvotes: 1

Related Questions