CSS 具有指定 HTML id 的 Html.ActionLink?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3622590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:42:02  来源:igfitidea点击:

Html.ActionLink with a specified HTML id?

cssasp.net-mvcasp.net-mvc-2html-helperactionlink

提问by Peter

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

我想给出一个Html.ActionLink用 HTML id生成的类似内容,以便我可以根据我所在的位置更改 CSS。我有一MasterPage组链接,我想通过 Jquery 更改该活动 #id 的 css 来区分活动的“Tab”

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>

回答by Kelsey

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 } expectederror. The following is what you are looking for:

你走在正确的轨道上。我不确定为什么它对你不起作用,因为你的代码有一个会产生} expected错误的错字。以下是您要查找的内容:

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

Which produces teh following HTML:

它产生以下 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 idhtml element. Your probably passing the new { id="blah" }param into the routeValuesparameter, which will cause it to be used when building the route link, rather than the htmlAttributesparamater which is what you want.

编辑:我刚刚意识到问题所在,因为我误读了您尝试过的内容。您正在使用错误的重载来传递idhtml 元素。您可能将new { id="blah" }参数传递给routeValues参数,这将导致在构建路由链接时使用它,而不是htmlAttributes您想要的参数。

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 htmlAttributesparam.

这确保new { id="blah" }被传递到htmlAttributes参数中。

回答by Peter

Try this:

尝试这个:

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

回答by MrBliz

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

您正在将 id 属性传递给 routevalue,这就是为什么它给您有趣的 href。将 null 传递给路由值,然后添加您的 htmlattributes

回答by Benja

try this

尝试这个

@Html.ActionLink("Forgot your access?", "RecoverPassword", 
"Account", new { area = "registration-full.html" }, 
new { @class = "col-xs-6", id = "login-forget-link" })