asp.net mvc - Validation of multiple viewmodels inside the same ActionResult -
i new mvc. have actionresult accepts requests clients, , attempting employ mvc validation , model-binding magic. action not return view, redirects other actions per logic.
my issue there different types of requests associated different models, , these models mutually exclusive.
example:
class model1 { [required] public string propa; } class model2 { [required] public string propb; }
the logic inside actionresult follow along likes of:
if model1 valid redirecttoaction("a") elseif model2 valid redirecttoaction("b") else redirecttoaction("error")
what best practice implementing type of functionality, or similar in asp.net mvc4?
you should able create validationcontext , validate each viewmodel in controller. you'd have following each viewmodel:
var validationresults = new list<validationresult>(); var context = new validationcontext(myobject); bool validationpassed = validator.tryvalidateobject(myobject, context, validationresults, true);
Comments
Post a Comment