在属性名 - 用一个自定义属性DropDownListFor? [英] DropDownListFor with a custom attribute with - in attribute name?
问题描述
问:我需要创建一个DropDownList是这样的:
Question: I need to create a dropdownlist like this:
<select id="ddCustomers" data-placeholder="Choose a customer" class="chzn-select" style="width:350px;" tabindex="1" multiple>
现在,我可以添加自定义属性是这样的:
Now I can add custom attributes like this:
@Html.DropDownListFor(m => m.Id, Model.Values, new { @disabled = "disabled" })
不幸的是,如果有一个 - 在变量名,那么它不编译
Unfortunately, if there is a "-" in the variable name, then it doesn't compile.
@Html.DropDownListFor(m => m.Id, Model.Values, new { @disabled = "disabled", @data-placeholder = "whatever" })
和怎么样的多,它没有属性值?
And what about the multiple, which has no attribute value ?
如果我传递一个字典或NameValueColletion代替新{@disabled =已禁用}
,那么它输出NameValueColletion /词典的属性...
If I pass a Dictionary or a NameValueColletion instead of the new { @disabled = "disabled" }
, then it outputs the properties of the NameValueColletion/Dictionary...
我如何将属性与属性键特殊字符?
How can I pass attributes with special characters in the attribute key ?
推荐答案
使用下划线代替
@data_placeholder = "whatever"
由于MVC3_替换为 - 渲染时。
Since Mvc3 "_" is replaced with "-" when rendered.
作为底线不是在HTML属性名接受这工作得很好。
This works fine as underscores are not acceptable in attribute names in html.
这篇关于在属性名 - 用一个自定义属性DropDownListFor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!