ASP.NET MVC3 TryValidateModel validates entire model collection, not just single instance -


i have action takes list of models. i'd validate each 1 individually vs. entire model collection @ once. i'm trying use tryvalidatemodel, appears if 1 of models invalid, of them invalid. form displays 5 surveyresponsemodels (a class 2 required strings , 2 ints). if fill out 5 models completely, validcount = 5 below. however, if of 5 models incomplete (thus failing validation), validcount of 0. expected behavior of tryvalidatemodel? if so, ideas on how can validate these 1 @ time?

    [httppost]     public actionresult create(ilist<surveyresponsemodel> respondents)     {         int validcount = 0;          foreach (surveyresponsemodel respondent in respondents)         {             if (tryvalidatemodel(respondent))             {                 validcount++;             }         }         modelstate.addmodelerror("", validcount.tostring() + " respondents passed validation");     } 

looking @ code, appears me tryvalidatemodel validate models of type given object provided, not particular object itself. moreover, returns current value of modelstate.isvalid property once there invalid model, invocations of tryvalidatemodel return false. if want this, think you'll need , run validators each model instance on particular model instance.

i think model validators have been run time invoking them manually. can check (for invalid model) looking @ value of modelstate.isvalid before loop. if it's false, means validators run model binder, think happens.

you can find source code mvc @ http://aspnet.codeplex.com/.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -