How to set numeric editorfor min, max and default

2019-06-16 01:22发布

I have an int value that I want to render as a numeric up down with an id that is"Quantity" , so I do the following in razor:

<div class="field-label">@Html.LabelFor(m => model.Quantity)</div>
<div class="field-editor">@Html.EditorFor(m => model.Quantity, null, "Quantity")</div>

In chrome this gives me the right UI, however I would like to set the min, max and default value so it works like the following code does.

<input id="Quantity" type="number" name="quantity" min="0" max="10" value="0" >

1条回答
\"骚年 ilove
2楼-- · 2019-06-16 01:39

You can do this via the "object AddiditonalViewData" overload of the @Html.EditorFor function. Then specify htmlAttributes like so:

@Html.EditorFor(m => Model.Quantity, new {htmlAttributes = new {min = 0, max = 20} } )

Note that the "value" is set to the actual value of the Quantity property, no matter what you define in the htmlAttributes.

查看更多
登录 后发表回答