[英]Adding a hyphen to the html attribute name using MVC3 WebGrid helper
我在尝试将自定义HTML5数据属性添加到使用WebGrid帮助器呈现的表时遇到问题。 我希望表标签看起来如下:
<table data-test="testdata"><!-- Table Content --></table>
以下是使用Razor视图引擎的示例视图:
@{
var myUser = new
{
Id = 1,
Name = "Test User"
};
var users = new[] { myUser };
var grid = new WebGrid(users);
}
@grid.GetHtml(htmlAttributes: new { data-test = "testdata"})
最后一行将生成“无效的匿名类型成员声明符”。 错误,因为数据测试中的连字符。
对于其他一些输入HtmlHelpers,您可以使用下划线代替连字符,并且在渲染时它将自动更改为连字符。 WebGrid不会发生这种情况。
如果我传入htmlAttributes的字典:
@grid.GetHtml(htmlAttributes: new Dictionary<string, object> {{ "data-test", "testdata"}})
表格如下呈现:
<table Comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]" Count="1" Keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" Values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]"><!-- Table Content --></table>
我做错了什么,我该怎么做才能根据需要渲染属性?
我担心这是不可能的。 不幸的是,WebGrid不支持与标准HTML帮助程序相同的语法,例如TextBoxFor
,您可以:
@Html.TextBoxFor(x => x.SomeProp, new { data_test = "testdata" })
并且下划线将自动转换为破折号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.