Skip to content

Commit

Permalink
ModelState.ToSerializableDictionary now handles multiple error keys. F…
Browse files Browse the repository at this point in the history
…ixes #15.
  • Loading branch information
benfoster committed Mar 6, 2013
1 parent 0d5e04c commit 51d5232
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Fabrik.Common.Web/ModelStateExtensions.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ namespace Fabrik.Common.Web
public static class ModelStateExtensions public static class ModelStateExtensions
{ {
/// <summary> /// <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> /// </summary>
public static IDictionary<string, string> ToSerializableDictionary(this ModelStateDictionary modelState) public static IDictionary<string, string[]> ToSerializableDictionary(this ModelStateDictionary modelState)
{ {
Ensure.Argument.NotNull(modelState, "modelState"); return modelState.Where(x => x.Value.Errors.Any()).ToDictionary(

kvp => kvp.Key,
var dictionary = (from k in modelState.Keys kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
from e in modelState[k].Errors );
select new { k, e.ErrorMessage }).ToDictionary(x => x.k, x => x.ErrorMessage);

return dictionary;
} }
} }
} }

0 comments on commit 51d5232

Please sign in to comment.