Skip to content

Instantly share code, notes, and snippets.

@danstuken
Created September 11, 2012 16:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danstuken/3699804 to your computer and use it in GitHub Desktop.
Save danstuken/3699804 to your computer and use it in GitHub Desktop.
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
Copy link

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