asp.net-mvc - 如何在@Html.TextBox mvc4 中同时添加css class 和id

标签 asp.net-mvc asp.net-mvc-4

我需要同时在@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" />

最佳答案

正确 overload method

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/

相关文章:

c# - Entity Framework - 导航属性不保存

c# - 路径 '/' 的 Controller 未找到或未实现 IController

Controller 操作的基于 ASP.NET MVC 角色的权限

javascript - 单击“确定”后 jQuery 警报消息框重定向

jquery - 单击按钮上的 ajax 调用后,部分 View 刷新不起作用

c# - 使用 html 内容创建链接

asp.net-mvc - 使用模型和 ViewData 项创建 ViewDataDictionary 的简写?

asp.net-mvc - 如何为每个请求始终设置默认路由

jquery - 如何在 MVC 5 项目中添加 jQueryUI 库?

c# - 尝试使用 ASP.NET MVC 5 中的 MemoryStream 发送电子邮件时无法访问已关闭的流