问题描述
我正在使用MVC3的WebGrid,并希望添加表格数据-XXX的自定义属性的<表>
元素通过<$ C $呈现C> WebGrid.GetHtml()。
I am using an MVC3 WebGrid, and want to add custom attributes of the form "data-xxx" to the <table>
element rendered by WebGrid.GetHtml()
.
我试图做到这一点,如下所示:
I tried to do this as follows:
grid.GetHtml(...
htmlAttributes: new { data_xxx = "value" }
);
然而,这呈现为:
However this renders as:
<table ... data_xxx="value">
代替
预期的:
instead of the expected:
<table ... data-xxx="value">
即。不像其他的MVC助手,它没有连字符替换属性名下划线。
I.e. unlike other MVC helpers, it doesn't replace the underscore in the property name with a hyphen.
在进行位,且反射洞穴探险的好像是:
After a bit of spelunking with Reflector it seems that:
-
WebGrid.GetHtml()
电话System.Web.WebPages.Html.ObjectToDictionary
来转换匿名属性对象字典。不同于方法System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes
使用其他帮助,这并不能代替_为 - 。
WebGrid.GetHtml()
callsSystem.Web.WebPages.Html.ObjectToDictionary
to convert the anonymous attributes object to a dictionary. Unlike the methodSystem.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes
used by other helpers, this doesn't replace "_" by "-".
与大多数其他MVC佣工, WebGrid.GetHtml()
不具有过载,它接受一个的IDictionary&LT;字符串,对象&gt;
,所以无法通过指定属性以这种方式解决该问题。
Unlike most other MVC helpers, WebGrid.GetHtml()
does not have an overload that takes an IDictionary<string, object>
, so I can't work around the bug by specifying the attributes in this way.
这看起来像我的错误,我已经提交给连接:
This looks like a bug to me, and I've submitted it to Connect:
http://connect.microsoft.com/VisualStudio/feedback/details/767456/webgrid-gethtml-helper-does-not-allow-data-attributes-to-be-set
有一种变通方法有什么建议?抑或是固定MVC4?
Any suggestions for a workaround? Or is it fixed in MVC4?
推荐答案
感谢您报告此问题:
这是通过解决问题610安培昨日定格在codePLEX; 575。
This was fixed yesterday in CodePlex by resolving issues 610 & 575.
的https://aspnetwebstack.$c$cplex.com/workitem/610
的https://aspnetwebstack.$c$cplex.com/workitem/575
您可以试用夜间位。
这篇关于添加自定义属性表进行的WebGrid呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!