I am trying to figure out if there is a way to make sure that only numerical input is allowed using data annotations and Entity Framework.
I am using the following code
[Required] [DisplayName("Client No")] [Column("client_no", TypeName = "smallint")] public virtual Int16 Number { get; set; }
I want this to be displayed using a class of numbers.
In one place I use the following
<input type="number" name="searchClientNo" class="numericOnly" /><br />
but in the input form I use
@Html.EditorFor(m => m.Number, EditorTemplate.TextBox)
where I have a custom editor for the following code
<div class="editor-label"> @Html.Label((ViewData.ModelMetadata.DisplayName??ViewData.ModelMetadata.PropertyName), new Dictionary<string, object> { { "for", ViewData.ModelMetadata.PropertyName } }) </div> <div class="editor-field"> @Html.TextBox("", (object)Model, new Dictionary<string, object> { { "id", ViewData.ModelMetadata.PropertyName }, { "name", ViewData.ModelMetadata.PropertyName }, { "class", "text-box single-line"}, { "data-bind", "value: " + ViewData.ModelMetadata.PropertyName }, }) @Html.ValidationMessage(ViewData.ModelMetadata.PropertyName, new Dictionary<string, object> { { "data-valmsg-for", ViewData.ModelMetadata.PropertyName } }) </div>
I am wondering how I can keep this code intact, but still use a text text box. Do I need to use UIHint?
Or, alternatively, is it possible to make my existing EditorFor smarter?
I found this blog post http://robseder.wordpress.com/2012/06/01/uihint-displaytemplates-and-editortememplates-in-mvc/ , but I already use a custom editor. Maybe I need to add a new type, say, EditorTemplate.NumericTextBox and add another editor? It seems this might work, I'm going to try it tomorrow ...
Thank you very much in advance.