Html.ActionLink with a specified HTML id?
Asked Answered
B

4

41

I'd like to give the like generated with an Html.ActionLink an HTML id so I can change the CSS depending on where I am. I have a MasterPage with a set of links and I'd like to distinguish the active "Tab" with Jquery changing the css of that active #id

Right now I'm using:

<%: Html.ActionLink("Some View", "Index", "controller")%>

It generates:

<a href="/controller">Some View</a>

I'd like to generate:

<a id="something" href="/controller">Some View</a>

Is that possible? I've tried:

<%: Html.ActionLink("Some View", "Index", "controller", new {id="blahbla")%>

But that generates:

<a href="/controller/Length?5">Some View</a>
Brand answered 1/9, 2010 at 22:31 Comment(2)
I am not familiar with the correct syntax, but should that { really not be closed?Abagail
It should, but i would have thought the web server would have generated an error, when they tried to run that web page. I think it's a typo. Can the original poster confirm please?Axseed
J
56

You were on the right track. I am not sure why it didn't work for you as your code has a typo which would have produced a } expected error. The following is what you are looking for:

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

Which produces teh following HTML:

<a href="/SomeController/SomeAction" id="someID">Test Link</a>

Edit: I just realized what the issue is because I was mis-reading what you tried. You are using the wrong overload to pass in the id html element. Your probably passing the new { id="blah" } param into the routeValues parameter, which will cause it to be used when building the route link, rather than the htmlAttributes paramater which is what you want.

I think you are using:

ActionLink(string linkText, string actionName, Object routeValues,
    Object htmlAttributes)

When what you need to use is the following overload like I did above in my answer:

ActionLink(string linkText, string actionName, string controllerName,
    Object routeValues, Object htmlAttributes)

Which makes sure new { id="blah" } is being passed into the htmlAttributes param.

Jujitsu answered 1/9, 2010 at 23:6 Comment(0)
B
6

Try this:

<%: Html.ActionLink("Some View", "Index", "controller", null, new {id="something}")%>
Brand answered 1/9, 2010 at 22:52 Comment(0)
A
2

Basically it's giving an error because there is no method overload that has the signature you want.

The closest signature to the one that you need is

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

)

You are passing the id attribute into the routevalue which is why it's giving you the funny href. pass null into the routevalue, then add your htmlattributes

Axseed answered 1/9, 2010 at 22:57 Comment(0)
G
2

try this

@Html.ActionLink("Forgot your access?", "RecoverPassword", 
"Account", new { area = "registration-full.html" }, 
new { @class = "col-xs-6", id = "login-forget-link" })
Gibbie answered 10/12, 2014 at 19:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.