ASP.NET MVC2 不将 HtmlAttributes 中的下划线替换为破折号

发布于 2024-10-01 15:03:25 字数 644 浏览 5 评论 0原文

我从几个不同的来源听说,在 ASP.NET MVC2 中使用 HTML 帮助程序时,可以通过在其中使用下划线来创建带有破折号的自定义属性(例如 )破折号的位置,当 HTML 写入页面时,下划线将被破折号替换。

所以,像这样的东西:

<%= HtmlActionLink(Model.Name, "MyView", null, new {data_rowId = Model.id}) %>

应该渲染为

<a data-rowId="0" href="myURL">Row Name</a>

但是...事实并非如此。我认为也许这个功能只在 MVC3 Beta 预览版中启用(正如 MVC3 预览版发行说明中提到的那样),但是这个thread 是同样的事情,它是关于 MVC2 的。

我知道我可以使用该线程中提供的其他解决方案,但如果存在更优雅的解决方案,我宁愿不必诉诸使用字典。

有人知道我是否可以做一些简单的事情来让这个特定的事情发挥作用吗?

I've heard from a couple of different sources that when using HTML helpers in ASP.NET MVC2, one can create custom attributes with dashes in them (e.g. <a data-rowId="5">) by using an underscore in place of the dash, and when the HTML is written to the page the underscores will be replaced by dashes.

So, something like this:

<%= HtmlActionLink(Model.Name, "MyView", null, new {data_rowId = Model.id}) %>

should render as

<a data-rowId="0" href="myURL">Row Name</a>

But... it's not. I think that maybe this feature is only enabled in the MVC3 Beta preview (as it's mentioned in the MVC3 preview release notes), but this thread is about the same thing, and it's regarding MVC2.

I know I can use the other solution presented in that thread, but I'd rather not have to resort to using the dictionary if a more elegant solution exists.

Anyone know if there's something simple I can do to get this particular thing working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不知所踪 2024-10-08 15:03:25

不完全是最优雅的解决方案,但可能可以接受:

<%= Html.ActionLink(
    Model.Name, 
    "MyView", 
    null, 
    new Dictionary<string, string> { { "data-rowId",  Model.id } }
) %>

旁注:根据标准文档类型,data-rowId 是 HTML 中完全无效的属性,因此也许最优雅的解决方案是摆脱它:-)

Not exactly the most elegant solution but probably acceptable:

<%= Html.ActionLink(
    Model.Name, 
    "MyView", 
    null, 
    new Dictionary<string, string> { { "data-rowId",  Model.id } }
) %>

On a side note: data-rowId is a totally invalid attribute in HTML according to standard doctypes so maybe the most elegant solution would be to get rid of it :-)

请远离我 2024-10-08 15:03:25

System.Web.Mvc.HtmlHelper 提供了一个静态方法来执行您正在寻找的操作:

<%= Html.ActionLink(
    Model.Name, 
    "MyView", 
    null, 
    HtmlHelper.AnonymousObjectToHtmlAttributes(new { data_row_id: Model.id })
) %>

此方法将用连字符替换下划线 - 正如有人指出的那样,请务必使用全小写属性名称,符合 HTML5。

System.Web.Mvc.HtmlHelper provides a static method that does what you're looking for:

<%= Html.ActionLink(
    Model.Name, 
    "MyView", 
    null, 
    HtmlHelper.AnonymousObjectToHtmlAttributes(new { data_row_id: Model.id })
) %>

This method will will substitute underscores with hyphens - as somebody pointed out though, be sure to use all-lowercase attribute names, which are HTML5-compliant.

三五鸿雁 2024-10-08 15:03:25

考虑一下 ASP.NET MVC 的设计目的。它旨在让您严格控制所生成的 HTML。如果某些东西无法访问,您无需费力去修复它。您想要做的是创建无效的 HTML 或 XHTML。不要尝试将无效的 HTML 简化为仅使用 ID。可访问性指南的#1 是 HTML 对于声明的类型有效。

只要 ID 在文档中是唯一的,您就可以满足 HTML 一致性——并且它将使使用 jQuery 进行操作变得更加容易。

如果可以的话,您为什么要尝试创建无效的 HTML?或者这是一种专有的 XML 格式?

顺便说一句,MVC 3 RC 今天发布了。


编辑

使用HTML5功能,虽然“闪亮”和酷,但不稳定。该规范的许多部分每周都会发生变化。当最终规格发布时,现在认为好的东西可能就不好了。 MVC 3 确实有一些东西可以让你的工作更加友好,但现在你必须使用字典。

Consider what ASP.NET MVC is designed to do. It is designed to give you tight control over the HTML that you produce. If something is not accessible, you won't have to jump through hoops to fix it. What you are trying to do is create invalid HTML, or XHTML. Instead of trying to make HTML that is invalid simplify to just using an ID. #1 on the accessibility guidelines is that the HTML is valid for the declared type.

As long as the ID is unique within the document you will have satisfied HTML conformance--and it will make it much easier to manipulate with jQuery.

If I may, why are you trying to create invalid HTML? Or is this a proprietary XML format?

BTW, MVC 3 RC came out today.


Edit:

Playing with HTML5 features, while "shiny" and kool, are unstable. Many parts of the specification are changing as often as weekly. What is considered good now may not be good when the final spec is released. MVC 3 does have some things that will make your job more friendly, for now you have to use a dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文