This is a migrated thread and some comments may be shown as answers.

[Solved] Setting Min Max from Data Annotations in template MVC 2

2 Answers 220 Views
NumericTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Daniel
Top achievements
Rank 1
Daniel asked on 16 Mar 2011, 09:49 PM
I am using the following template code

<%= Html.Telerik().IntegerTextBox()
        .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
        .InputHtmlAttributes(new { style = "width:100%" })
        .Value(Model) 
%>


In my model I have a field defined as followed, DataType(Integer) is so it picks up the editor template from above.

[DataType("Integer")]
[Range(1, 999)]        
public int MyNumber{ get; set; }

When I go to edit a row of data in the grid, the numeric textbox is showing fine, but it allows me to use the "up"/"down" arrows below 0 and higher than 999. 

Basically when you use the numeric textbox as a edit template in a grid it is not picking up the range values from the model.  Is this standard behaviour or am I missing something?

Also we are using Telerik Extensions for ASP.NET MVC 2 2010.Q3.1110
Thanks
Daniel

2 Answers, 1 is accepted

Sort by
0
Daniel
Top achievements
Rank 1
answered on 17 Mar 2011, 02:52 PM
I figured it out last night...

The issue is because we are using an editing template.

When we bind the column to the model we are binding to column SheetCount.  When the grid displays, a LABEL is created with the name SheetCount which is bound to the model.  But when we edit the row, the editable field changes to a numeric textbox which obviously can not be named SheetCount as it conflicts with the lable which is shown when we display the grid so the numeric textbox gets the name SheetCount-Text.  So because the name has -Text appended to it, it cannot resolve the name to the model therefore it never picks up the data annotations.

Kos
0
murali
Top achievements
Rank 1
answered on 21 Apr 2011, 10:56 PM
Hello Daniel,

If you don't mind can you post your view code too, I am trying to do the same and it still doesn't work for, I checked the names, but the range is not validated. I am on latest version 2011.1.315

In my model I have a field called

        [DisplayName("Version")]
        [DataType("Integer")]
        [Range(1, 100, ErrorMessage="Version should be between 1 and 100")]
        public int Version { get; set; }

In my view 

<span class="editor-label">Version:</span>
<%: Html.EditorFor(m => m.Version) %> <%: Html.ValidationMessageFor(model => model.Version, "*")%>

And here is my template.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%= Html.Telerik().IntegerTextBox()
        .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
        //.MinValue(int.MinValue)
        //.MaxValue(int.MaxValue)
        .Value(Model) 
%>

I am not sure what I am doing wrong. Any help is greatly appreciated.

Thank you,
mm


Tags
NumericTextBox
Asked by
Daniel
Top achievements
Rank 1
Answers by
Daniel
Top achievements
Rank 1
murali
Top achievements
Rank 1
Share this question
or