Created
September 11, 2012 16:55
-
-
Save danstuken/3699804 to your computer and use it in GitHub Desktop.
Dump mvc ModelState errors to Debug console.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Dan appreciated. Good to see my blog post is being used!
http://garfbradazweb.wordpress.com/2011/09/05/mvc-3-modelstate-modelerrorcollection-debug/