Html.ActionLink与指定的HTML ID? [英] Html.ActionLink with a specified HTML id?

查看:819
本文介绍了Html.ActionLink与指定的HTML ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给与所产生的如 Html.ActionLink 一个HTML ID,所以我可以根据我在哪里更改CSS。我有一个母版用一组链接,我想用jQuery改变这种积极#ID的CSS来区分主动标签

现在,我使用:

 <%:Html.ActionLink(有些视图,索引,控制器)%GT;

它产生:

 < A HREF =/控制器>有的查看< / A>

我想生成:

 <一个ID =东西HREF =/控制器>有的查看< / A>

这可能吗?我试过:

 <%:Html.ActionLink(有些视图,索引,调节器,新{ID =blahbla)%>

但生成:

 < A HREF =/控制器/长度5?>有的查看< / A>


解决方案

您在正确的轨道上。我不知道为什么它不适合您,作为您code的工作这将产生一个}有望错误错字。以下是你在找什么:

 <%= Html.ActionLink(测试链接,SomeAction,SomeController
         空,新{ID =someID})%GT;

将会产生以下德HTML:

 < A HREF =/ SomeController / SomeActionID =someID>测试链接< / A>

编辑:我才意识到这个问题是什么,因为我是错读你尝试过什么。您正在使用错误的过载在 ID HTML元素来传递。您可能传递新{ID =嗒嗒} 参数到 routeValues​​ 参数,这将导致它是建立路由链路时,而不是 htmlAttributes paramater这是你想要的应用。

我想你使用的是:

 的ActionLink(字符串LINKTEXT,串actionName,对象routeValues​​,
    对象htmlAttributes)

当你需要使用的是以下过载像我一样在上面我的回答:

 的ActionLink(字符串LINKTEXT,串actionName,串controllerName,
    对象routeValues​​,对象htmlAttributes)

这可以确保新{ID =嗒嗒} 被传递到 htmlAttributes 参数。

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>

解决方案

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.

这篇关于Html.ActionLink与指定的HTML ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆