Permalink
Browse files

ModelState.ToSerializableDictionary now handles multiple error keys. …

…Fixes #15.
  • Loading branch information...
1 parent 0d5e04c commit 51d5232acf85fc4d10a190563dc569fb5ac01c21 @benfoster committed Mar 6, 2013
Showing with 6 additions and 9 deletions.
  1. +6 −9 src/Fabrik.Common.Web/ModelStateExtensions.cs
@@ -7,17 +7,14 @@ namespace Fabrik.Common.Web
public static class ModelStateExtensions
{
/// <summary>
- /// Converts the <paramref name="modelState"/> to a string dictionary that can be easily serialized.
+ /// Converts the <paramref name="modelState"/> to a dictionary that can be easily serialized.
/// </summary>
- public static IDictionary<string, string> ToSerializableDictionary(this ModelStateDictionary modelState)
+ public static IDictionary<string, string[]> ToSerializableDictionary(this ModelStateDictionary modelState)
{
- Ensure.Argument.NotNull(modelState, "modelState");
-
- var dictionary = (from k in modelState.Keys
- from e in modelState[k].Errors
- select new { k, e.ErrorMessage }).ToDictionary(x => x.k, x => x.ErrorMessage);
-
- return dictionary;
+ return modelState.Where(x => x.Value.Errors.Any()).ToDictionary(
+ kvp => kvp.Key,
+ kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
+ );
}
}
}

0 comments on commit 51d5232

Please sign in to comment.