如何在Razor中设置数值编辑器的最小值,最大值和默认值 [英] How to set numeric editorfor min, max and default values in Razor

查看:110
本文介绍了如何在Razor中设置数值编辑器的最小值,最大值和默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个int值,希望以数字形式向下呈现,其ID为"Quantity",所以我在剃刀中执行以下操作:

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>

在chrome中,这为我提供了正确的用户界面,但是我想设置最小值,最大值和默认值,使其像下面的代码一样工作.

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

推荐答案

您可以通过@ Html.EditorFor函数的对象AddiditonalViewData"重载来执行此操作.然后像这样指定htmlAttributes:

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} } )

请注意,无论您在htmlAttributes中定义什么,值"都将设置为数量"属性的实际值.

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

这篇关于如何在Razor中设置数值编辑器的最小值,最大值和默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆