How can i get the keys and errors from the ModelStateDictionary? I have this piece of code, but it's done too many times and it throws me a duplicate key error.
if (!modelstate.IsValid)
{
var errMessages = new Dictionary<string, string>();
foreach (var item in modelstate.Keys)
{
foreach (var item2 in modelstate.Values)
{
foreach (var item3 in item2.Errors)
{
errMessages.Add(item.ToLower(), item3.ErrorMessage);
}
}
}
return new Error { StatusCode = 409, ErrorMessages = errMessages };
}
Thanks for all answers, have a nice day :D.
I managed to solve this problem, but if someone has better suggestions, I would like to read it.
if (!modelstate.IsValid)
{
var errMessages = new Dictionary<string, string>();
var i = 0;
foreach (var item2 in modelstate.Values.SelectMany(p => p.Errors))
{
var key = modelstate.Keys.ToArray();
errMessages.Add(key[i].ToLower(), item2.ErrorMessage);
i++;
}
return new Error { StatusCode = 409, ErrorMessages = errMessages };
}