添加自定义属性表进行的WebGrid呈现 [英] Add custom attributes to table rendered by WebGrid

查看:120
本文介绍了添加自定义属性表进行的WebGrid呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC3的WebGrid,并希望添加表格数据-XXX的自定义属性的&LT;表&gt; 元素通过<$ 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:

  1. WebGrid.GetHtml()电话 System.Web.WebPages.Html.ObjectToDictionary 来转换匿名属性对象字典。不同于方法 System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes 使用其他帮助,这并不能代替_为 - 。

  1. WebGrid.GetHtml() calls System.Web.WebPages.Html.ObjectToDictionary to convert the anonymous attributes object to a dictionary. Unlike the method System.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:

<一个href="http://connect.microsoft.com/VisualStudio/feedback/details/767456/webgrid-gethtml-helper-does-not-allow-data-attributes-to-be-set" rel="nofollow">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呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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