ModelState is not Valid

When you are using MVC you will come across using the ModelState, and the most common use is ModelState.IsValid, if you've not used this before then check out Validating Model Data in an MVC Application or xVal - a validation framework for ASP.NET MVC, what I did come across is an exception beyound the normal checks, that a table was missing from the database.  Although the error was recorded in the ModelState, it did not raise any exception and hence the exception handling did not capture the message.  To record the Exception you'll need to go over the collection and this code allows you to do this:

 

foreach (var value in ModelState.Values)

{

     foreach (var error in value.Errors )

     {

        throw new Exception (error.ErrorMessage, error.Exception);

     }

}

 

 

blog comments powered by Disqus

About the author

You have probably figured out by now that my name is Bryan Avery (if not, please refer to your browser's address field).  Technology is more than a career to me - it is both a hobby and a passion.  I'm an ASP.NET/C# Developer at heart...

Month List