我有一个EditorFor
剃刀定义的输入框,用于设置绑定数据模型的“小时”的值:
@Html.EditorFor(modelItem => item.Hours, null, "weeklytargets[" + item.WeeklyTargetId + "].Hours")
当它向HTML吐出时,它看起来像是这样:
<input class="text-box single-line" data-val="true" data-val-number="The field Hours must be a number." id="weeklytargets_17__Hours" name="weeklytargets[17].Hours" type="number" value="10">
如何为数字类型的输入框添加属性min和max?
答案 0 :(得分:1)
尝试使用additionalViewData参数传入htmlAttributes。这样您就可以指定最小值和最大值:
@Html.EditorFor(
expression: modelItem => item.Hours,
templateName: null,
htmlFieldName: "weeklytargets[" + item.WeeklyTargetId + "].Hours",
additionalViewData: new
{
htmlAttributes = new
{
min = 0,
max = int.MaxValue
}
})
答案 1 :(得分:1)
这对我有用
@Html.TextBoxFor(m => m.years_completed, new { @type="number", min = "1", max = "10" })