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" >
Simply use Range DataAnnotation attribute on your model property.
TextBoxFor: It will render like text input html element corresponding to specified expression. In simple word it will always render like an input textbox irrespective datatype of the property which is getting bind with the control. EditorFor: This control is bit smart.
EditorFor<TModel,TValue>(HtmlHelper<TModel>, Expression<Func<TModel,TValue>>, String, String, Object) Returns an HTML input element for each property in the object that is represented by the expression, using the specified template, HTML field name, and additional view data.
Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With