c# - Validating a collection with Dataannotations -


just started on asp.net mvc, , i'm having trouble applying datannotations collection - i'm either missing basic, or going whole thing in wrong way.

i have viewmodel pass strongly-typed view:

public class addcostingviewmodel {       [required]       [range(120,190)]       public int somevalue {get; set;}        public list<grade> grades { get; set; }       public list<int> gradevalues { get; set; } } 

i have no idea if number of objects in either grades list remain constant in future, can't pass in "grade1, grade2" etc - passing in list , building cshtml page seems obvious way ensure have form contains possible grades, , returns right values. cshtml looks something this:

<tr>     @foreach (var g in model.grades)     {         <th>@g.gradename</th>     }     </tr>     <tr>      @for (int = 0; < model.grades.count(); i++)     {         <td>         <div class="editor-field">             @html.editorfor(model => model.gradevalues[i])             @html.validationmessagefor(model => model.gradevalues[i])         </div>         </td>     }     </tr> 

which handily generates table containing fields editing possible grade values. however, want validate these against range (as somevalue above), adding dataannotations above list (like did somevalue) doesn't seem work. possible or approach wrong altogether?

as seems, grade value not simple int, int domain specific restrictions. following logic, design new class holding grade value

public class gradevalue     {         [range(100, 200)]         public int value { get; set; }     } 

and in model, property describes list of grade values, be

public list<gradevalue> gradevalues { get; set; } 

and validation apply rande every gradevalue in viewmodel. , in view, not have change single line of code. moreover, design gradevalue class implicitely convertible int , vice versa, simpler usage.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -