如何在Razor中为min,max和default值设置数字编辑器

时间:2022-08-09 18:55:16

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:

我有一个int值,我想呈现为数字向下,ID为“数量”,所以我在剃刀中执行以下操作:

<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.

在chrome中,这给了我正确的UI,但是我想设置min,max和default值,使其像下面的代码一样工作。

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

1 个解决方案

#1


18  

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

您可以通过@ Html.EditorFor函数的“object AddiditonalViewData”重载来完成此操作。然后像这样指定htmlAttributes:

@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.

请注意,无论您在htmlAttributes中定义了什么,“value”都设置为Quantity属性的实际值。

#1


18  

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

您可以通过@ Html.EditorFor函数的“object AddiditonalViewData”重载来完成此操作。然后像这样指定htmlAttributes:

@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.

请注意,无论您在htmlAttributes中定义了什么,“value”都设置为Quantity属性的实际值。