我需要同时在@Html.TextBox mvc4 中添加css class 和id。
我试试
@Html.TextBox("name", new { id = "name"}, new { @class = "text-field" })
但结果我得到
<input class="text-field" id="name" name="name" type="text" value="{ id = name }">
我在这里不需要属性值。
我需要得到
<input type="text" value="" name="name" id="name" class="text-field" />
最佳答案
public static MvcHtmlString TextBox(
this HtmlHelper htmlHelper,
string name,
Object value,
Object htmlAttributes
)
您要使用
id
作为 HtmlAttribute,所以你应该在 HtmlAttributes 对象中使用它。 TextBox
的正确用法是:@Html.TextBox("name",
null,
new {
id = "name",
@class = "text-field"
})
如果您放置
id
在路由对象中,那么 id 将是一个路由值。
关于asp.net-mvc - 如何在@Html.TextBox mvc4 中同时添加css class 和id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16643928/