ASP.NET MVC,添加单个属性

时间:2014-03-31 20:34:35

标签: asp.net asp.net-mvc asp.net-mvc-3

对于attribute =“value”,我使用

@Html.TextBoxFor(x => x.Email, new { type = "email", @class = "form-control",
 placeholder="Email address" })
它给了我:

<input class="form-control" id="Email" name="Email" placeholder="Email address"
 type="email" value="" />

如何添加单个属性,因此我需要自定义和自动对焦:

<input class="form-control" id="Email" name="Email" placeholder="Email address" 
type="email" value=""  required autofocus />

谢谢!

1 个答案:

答案 0 :(得分:4)

如果您使用string.Empty,您将在生成的html中获得一个空属性

@Html.TextBoxFor(x => x.Email, new { type = "email", @class = "form-control",
placeholder = "Email address", required = string.Empty, autofocus = string.Empty })