我想为使用 Html.ActionLink
生成的类似内容提供一个 HTML id,这样我就可以根据我所在的位置更改 CSS。我有一个带有一组链接的 MasterPage
,我想用 Jquery 更改事件 #id 的 css 来区分事件的“Tab”
现在我正在使用:
<%: Html.ActionLink("Some View", "Index", "controller")%>
它产生:
<a href="/controller">Some View</a>
我想生成:
<a id="something" href="/controller">Some View</a>
这可能吗?我试过:
<%: Html.ActionLink("Some View", "Index", "controller", new {id="blahbla")%>
但这会产生:
<a href="/controller/Length?5">Some View</a>
最佳答案
您走在正确的轨道上。我不确定为什么它对您不起作用,因为您的代码有错字,会产生 } expected
错误。以下是您要查找的内容:
<%= Html.ActionLink("Test Link", "SomeAction", "SomeController",
null, new {id = "someID" }) %>
生成以下 HTML:
<a href="/SomeController/SomeAction" id="someID">Test Link</a>
编辑:我刚刚意识到问题所在,因为我误读了您的尝试。您使用了错误的重载来传递 id
html 元素。您可能将 new { id="blah"}
参数传递给 routeValues
参数,这将导致在构建路由链接时使用它,而不是 htmlAttributes
参数就是你想要的。
我认为您正在使用:
ActionLink(string linkText, string actionName, Object routeValues,
Object htmlAttributes)
当你需要使用的是下面的重载时,就像我在上面的回答中所做的那样:
ActionLink(string linkText, string actionName, string controllerName,
Object routeValues, Object htmlAttributes)
这确保 new { id="blah"}
被传递到 htmlAttributes
参数中。
关于css - 具有指定 HTML id 的 Html.ActionLink?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3622590/