2012-08-11 99 views
0
@Html.TextBox("UserName", null, new { /* ... */ }) 

我怎麼會添加像data-foo="bar"required到htmlattributes對象的屬性來形成輸入?添加HTML5屬性使用剃刀

感謝

回答

5

如果您正在使用MVC3,你可以在你的HTML屬性使用下劃線,它們被轉換爲破折號

@Html.TextBox("UserName", null, new { data_foo = "bar", required = "" }) 

HTML結果

<input data-foo="bar" id="UserName" name="UserName" required="" type="text" value=""> 

第二個選項。使用字典而不是匿名對象

@Html.TextBox("UserName", null, new Dictionary<string, object> { {"data-foo", "bar"}, {"required", ""} }) 
+0

謝謝,正是我所需要的 – Johan 2012-08-11 13:00:40

+0

是否有可能添加一個沒有對象值的鍵?由於'required'標記是'required'而不是'required =「」'? – Johan 2012-08-11 13:03:36

+0

但'required =「」'也是合適的語法。看[HTML5必填屬性](http://www.w3schools.com/html5/att_input_required.asp) – krolik 2012-08-11 13:47:53