ModelState.IsValid vs IValidateableObject in MVC3?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

There isn't anything more than that you just have to add it to the model you're validating. Here's an example of validation public class User : IValidatableObject { public Int32 UserID { get; set; } public string Name { get; set; } public IEnumerable Validate(ValidationContext validationContext) { //do your validation return new List(); } } And your controller would use this model public ActionResult Edit(User user) { if (ModelState. IsValid) { } } Hope this helps.

Other requirements are . Net 4 and data annotations - which you obviously need jsut for ivalidatableobject. Post any issues and we'll see if we can't resolve them - like post your model and your controller...you might be missing something.

4 You are correct, the one caveat is that if you have any validation attributes that result in the model being invalid, Validate never gets called. That was my issue. – Master Morality Sep 19 '10 at 16:13 Ah, how interesting.

Thanks for that. – BuildStarted Sep 19 '10 at 18:37 That caveat strikes me as a very handy thing to know :) +1 to both – Darko Z May 11 at 4:58.

Validation using the DefaultModelBinder is a two stage process. First, data annotations are validated. Then (and only if the data annotations validation resulted in zero errors), IValidatableObject.Validate() is called.

This all takes place automatically when your post action has a viewmodel parameter. ModelState. IsValid doesn't do anything as such.

Rather it just reports whether any item in the ModelState collection has non-empty ModelErrorCollection.

I always wondered about that. Whether validation was called automagically in the DefaultModelBinder, or if ModelState. IsValid checked some hidden _hadBeenValidated property and called validate if _hasBeenValidated == false.

– Master Morality Jan 29 at 15:49 The described behavior isn't exactly the real one: please see stackoverflow. Com/questions/8153602/… – Diego Nov 16 at 14:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions