FrontPsych
10/26/2017 - 9:58 AM

ASP.NET MVC ModelState errors debug pretty print

Debug print ModelState errors by key

foreach (var modelStateKey in ViewData.ModelState.Keys)
{
	var modelStateVal = ViewData.ModelState[modelStateKey];
	foreach (var error in modelStateVal.Errors)
	{
		var key = modelStateKey;
		var errorMessage = error.ErrorMessage;
		var exception = error.Exception;
		Debug.WriteLine($"key: {key}, errorMessage: {errorMessage}, exception: {exception}");
	}
}