我有一个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?
最佳答案 尝试使用additionalViewData参数传入htmlAttributes.这样您就可以指定最小值和最大值:
@Html.EditorFor(
expression: modelItem => item.Hours,
templateName: null,
htmlFieldName: "weeklytargets[" + item.WeeklyTargetId + "].Hours",
additionalViewData: new
{
htmlAttributes = new
{
min = 0,
max = int.MaxValue
}
})