css - 具有指定 HTML id 的 Html.ActionLink?

标签 css asp.net-mvc asp.net-mvc-2 html-helper actionlink

我想为使用 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/

相关文章:

javascript - 一次滚动左 div 和右 div

c# - 如何将参数传递给 ValidationAttribute?

asp.net-mvc - 我的 MVC 5 模板中没有 ApplicationRoleManager 类

asp.net-mvc - 如何在 ASP .NET MVC 中对参数进行 URL 编码

html - 如何使用 html 和 css 并排放置两个选择属性(以不同的形式)

html - asp.net 中 ie 10 和 mozilla 的条件 css

css - 如何从 ASP.NET MVC 局部 View (服务器端)注入(inject) CSS 文件?

c# - asp net mvc 验证和 ViewModel

css - 如何使用内联链接设置两个图像

asp.net-mvc - 一种在 aspnetboilerplate 中进行分页和排序的简单方法