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);
}
}