如何在@ Html.TextBox mvc4中添加新的css类

Hem*_*oni 21 asp.net-mvc asp.net-mvc-4

我使用以下代码来添加css类,@Html.TextBox但这只适用于@Html.TextBoxFor而不是@Html.TextBox.

@Html.TextBox("ticket_new_attachment_attributes_0_description", new { @class= "bigfield"})
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

ssi*_*777 51

试试这个

@Html.TextBox("ticket_new_attachment_attributes_0_description", null, new { @class= "bigfield"})
Run Code Online (Sandbox Code Playgroud)

  • 这段代码确实有效,但请注意Mario Sannum在下面的回答中给出了原因.第二个参数是value,第三个参数是属性. (2认同)

Mar*_*num 5

第二个参数是value
您需要对html属性的第三个参数使用重载,如下所示:

// Pass null (or the value) as second parameter
@Html.TextBox("ticket_new_attachment_attributes_0_description", null, new { @class = "bigfield"})
Run Code Online (Sandbox Code Playgroud)

请参阅msdn参考