andrewlocatelliwoodcock

Thoughts on Software

Accessing Model validation errors from the Controller

with 16 comments


MVC performs automatic model validation whilst it is binding the model, so as soon as we hit the Controller action method that is being called, we have access to a validated model. In fact, as discussed in my last post, we actually have access to the results of model validation in the method’s ActionFilters, including the first ActionFilter: OnActionExecuting, which fires before we even reach the action method.

MVC Exposes the results of this validation through the Controller’s ViewData.ModelState property. This is directly accessible in the Controller by calling ModelState and is most often used to check whether a model passed validation:

 

if (ModelState.IsValid)
{
    // model passed validation, so do some work here
    ...
}

 

But if ModelState.IsValid returns false, ie the model failed validation, how can we see the collection of errors?

In addition to the IsValid property, ModelState also contains an IEnumerable of KeyValuePairs of type <string, ModelState>. The ModelState object has two properties Key and Value. Key is an id for the ModelState (and is often property name of the property which failed validation but this depends on the validator that trapped the validation failure) and Value is the ModelState object itself and contains a collection of zero or more ModelError objects. Got all that?!

Now that’s all very well and good but once we know that a model has failed validation we may want to do something like find the properties with errors and log them, for example. A bit of LINQ magic can help us here: we can use LINQ to give us a List of ModelState objects that have an error. The LINQ code?

 

    var errors = ModelState.Where(state => state.Values.Errors.Count > 0).ToList()

 

This will give us a generic List<ModelState> object called errors that contains only the properties that have errors to do with as we please.

 

Written by andrewlocatelliwoodcock

December 16, 2011 at 20:17

16 Responses

Subscribe to comments with RSS.

  1. As I website possessor I believe the content material here is rattling great , appreciate it for your hard work. You should keep it up forever! Best of luck. 740880

    My Homepage

    December 22, 2011 at 01:14

  2. … [Trackback]…

    […] Informations on that Topic: andrewlocatelliwoodcock.com/2011/12/16/accessing-model-validation-errors-from-the-controller/ […]…

    My Homepage

    December 22, 2011 at 01:40

  3. Can’t say enough about this website – its alot better than mine

    Diane

    December 23, 2011 at 09:58

  4. wonderful post, very informative. I wonder why the other experts of this sector do not notice this. You must continue your writing. I am sure, you have a huge readers’ base already!

    Sheilah Jacque

    December 28, 2011 at 17:40

    • I will be ptuting this dazzling insight to good use in no time.

      Nyanna

      January 9, 2012 at 20:29

  5. Hiya. Mainly wanted to leave a swift remark and tell you that I’ve really liked scanning your personal web page and will be recommending it to my friends. Keep up the good work! Many thanks.

    Clement Janiszewski

    December 28, 2011 at 18:40

    • Gee whiz, and I thought this would be hard to find out.

      Dell

      January 10, 2012 at 05:59

  6. My brother recommended I might like this website. He was totally right. This post actually made my day. You can not imagine simply how much time I had spent for this information! Thanks!

    Eusebio Gwirtz

    December 29, 2011 at 15:42

  7. Hello there, simply changed into aware of your blog thru Google, and located that it is really informative. I’m gonna be careful for brussels. I’ll be grateful if you happen to continue this in future. Numerous folks will probably be benefited out of your writing. Cheers!

    Augustina Scarver

    January 4, 2012 at 00:40

    • Frankly I think that\’s asbolutley good stuff.

      Ireland

      January 9, 2012 at 22:39

  8. You actually make it appear so easy together with your presentation but I find this topic to be actually one thing which I think I might never understand. It sort of feels too complicated and very large for me. I am looking forward to your subsequent post, I’ll try to get the dangle of it!

    click here

    January 4, 2012 at 02:20

  9. I could read a book about this without finding such real-world approaches!

    Bella

    January 9, 2012 at 23:03

  10. What’s up, all is going perfectly here and ofcourse every one is sharing data, that’s truly excellent, keep up writing.

    Joel Bernheim

    January 10, 2012 at 00:19

  11. Hello there, I found your site via Google while looking for a related topic, your web site came up, it looks great. I have bookmarked it in my google bookmarks.

    Lesley Gallicchio

    January 10, 2012 at 17:49

  12. i love your post. it rully gives me more knowledge. keep posting like that….

    seo optimizare

    January 11, 2012 at 02:09

  13. Nice insightful article. I never thought that it was this simple after all. I had spent a good deal of my time searching for some great mind to explain this matter clearly to me and you are the only one that ever did that. Great big thanks! Have a great day.

    fp

    January 13, 2012 at 15:57


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: