Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dump mvc ModelState errors to Debug console.
using System.Linq;
using System.Web.Mvc;
public static class ModelStateDictionaryExtensions
{
#if DEBUG
public static void DumpErrors(this ModelStateDictionary modelState)
{
var errors = modelState.Where(a => a.Value.Errors.Count > 0)
.Select(b => new { b.Key, b.Value.Errors })
.ToArray();
foreach (var modelStateErrors in errors)
{
System.Diagnostics.Debug.WriteLine("Errors in {0}", modelStateErrors.Key);
foreach(var error in modelStateErrors.Errors)
System.Diagnostics.Debug.WriteLine("\tError: {0} ({1}) ", error.ErrorMessage, error.Exception == null ? "none" : error.Exception.Message);
}
}
#else
public static void DumpErrors(this ModelStateDictionary modelState)
{}
#endif
}
@garfbradaz

This comment has been minimized.

Copy link

commented Sep 14, 2012

Thanks Dan appreciated. Good to see my blog post is being used!

http://garfbradazweb.wordpress.com/2011/09/05/mvc-3-modelstate-modelerrorcollection-debug/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.