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.
我从几个不同的来源听说,当在ASP中使用HTML helper时。NET MVC2可以使用下划线代替破折号来创建带有破折号的自定义属性(例如),当HTML被写入页面时,下划线将被破折号替换。
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.
但是…它不是。我认为这个特性可能只在MVC3 Beta预览版中启用(正如MVC3预览版说明中提到的),但是这个线程是差不多的,它是关于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?
有人知道如果我能做一些简单的事情来让这个东西工作吗?
3 个解决方案
#1
4
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 :-)
附注:数据rowid是HTML中一个完全无效的属性,根据标准文档类型,所以可能最优雅的解决方案是删除它:-)
#2
3
System.Web.Mvc.HtmlHelper
provides a static method that does what you're looking for:
System.Web.Mvc。HtmlHelper提供了一个静态方法,可以执行您要查找的内容:
<%= 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.
这个方法将用连字符替换下划线——正如有人指出的那样,请确保使用全小写的属性名,这是与html5兼容的。
#3
0
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.
考虑什么ASP。NET MVC的设计就是这样做的。它的设计目的是让您严格控制所生成的HTML。如果某些东西是不可访问的,那么您就不需要通过跳圈来修复它。您要做的是创建无效的HTML或XHTML。在可访问性指导方针中,HTML对声明的类型是有效的,而不是试图将无效的HTML简化为仅仅使用ID。
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.
只要ID在文档中是惟一的,就可以满足HTML的一致性——它将使使用jQuery更容易操作。
If I may, why are you trying to create invalid HTML? Or is this a proprietary XML format?
如果可以,为什么要创建无效的HTML?或者这是一种专有的XML格式?
BTW, MVC 3 RC came out today.
顺便说一下,MVC 3 RC今天出来了。
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.
使用HTML5特性,而“闪亮”和kool则是不稳定的。规范的许多部分每周都在变化。当最终的规格发布时,现在被认为是好的可能不是好的。MVC 3确实有一些使您的工作更友好的东西,现在您必须使用字典。
#1
4
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 :-)
附注:数据rowid是HTML中一个完全无效的属性,根据标准文档类型,所以可能最优雅的解决方案是删除它:-)
#2
3
System.Web.Mvc.HtmlHelper
provides a static method that does what you're looking for:
System.Web.Mvc。HtmlHelper提供了一个静态方法,可以执行您要查找的内容:
<%= 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.
这个方法将用连字符替换下划线——正如有人指出的那样,请确保使用全小写的属性名,这是与html5兼容的。
#3
0
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.
考虑什么ASP。NET MVC的设计就是这样做的。它的设计目的是让您严格控制所生成的HTML。如果某些东西是不可访问的,那么您就不需要通过跳圈来修复它。您要做的是创建无效的HTML或XHTML。在可访问性指导方针中,HTML对声明的类型是有效的,而不是试图将无效的HTML简化为仅仅使用ID。
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.
只要ID在文档中是惟一的,就可以满足HTML的一致性——它将使使用jQuery更容易操作。
If I may, why are you trying to create invalid HTML? Or is this a proprietary XML format?
如果可以,为什么要创建无效的HTML?或者这是一种专有的XML格式?
BTW, MVC 3 RC came out today.
顺便说一下,MVC 3 RC今天出来了。
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.
使用HTML5特性,而“闪亮”和kool则是不稳定的。规范的许多部分每周都在变化。当最终的规格发布时,现在被认为是好的可能不是好的。MVC 3确实有一些使您的工作更友好的东西,现在您必须使用字典。