using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.ModelBinding;
namespace BuddyFinance.WebApi.Extensions
{
///
/// Model State Extensions
///
public static class ModelStateExtensions
{
///
/// Gets ModelState Errors
///
///
///
public static string GetValidationErrors(this ModelStateDictionary modelState)
{
var errorString = "";
if (!modelState.IsValid)
{
var errorsDictionary = modelState.Where(m => m.Value.Errors.Any())
.Select(m => new { m.Key, m.Value.Errors });
foreach (var item in errorsDictionary)
{
foreach (var error in item.Errors)
{
errorString += $"{item.Key}: {error.ErrorMessage}\n";
}
}
}
return errorString;
}
}
}