3

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>
Karl von Moor
  • 8,484
  • 4
  • 40
  • 52
Thiago
  • 1,547
  • 3
  • 25
  • 40

2 Answers2

9

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"})
Iridio
  • 9,213
  • 4
  • 49
  • 71
  • Great! Tks, mas.I Leave an empty space in text Link to show an image as a link: @Html.Action(" ", Action,"Controller", null, new{id="tec"}). It solved my problem! Tks! – Thiago Sep 20 '11 at 18:18
1

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.

Hari Gillala
  • 11,736
  • 18
  • 70
  • 117