这是我在MVC 5中的代码:
@Html.EditorFor(model => model.myfloatvalue,new { @type = "number",@min = "0",@step = "0.01",@value = "0" })
这是HTML代码:
<input class="text-Box single-line" data-val="true" data-val-number="The field Fix Amount must be a number." data-val-required="The Fix Amount field is required." id="myfloatvalue" name="myfloatvalue" type="text" value="">
不要
<input class="text-Box single-line" data-val="true" data-val-number="The field Fix Amount must be a number." data-val-required="The Fix Amount field is required." id="myfloatvalue" name="myfloatvalue" type="number" min="0" step="0.01" value="0">
我该怎么办?
谢谢你的回复!
解决方法
您是否尝试将匿名对象包装在另一个匿名对象的htmlAttributes中?使用EditorFor / TextBoxFor时,我相信MVC 5是影响编辑器输出的HTML属性的唯一方法.
@Html.EditorFor(model => model.myfloatvalue,new { htmlAttributes = new { @type = "number",@value = "0" }})
如果您不使用MVC-5.1或更高版本,则需要使用TextBoxFor().
注意这里没有使用htmlAttributes:
@Html.TextBoxFor(m => m.myfloatvalue,new { type = "number",min = "0",step = "0.01" }) // don't set the value attribute