Returning a list of keys with ModelState errors
Asked Answered
M

2

5

How can I return a list/array of all keys that have an error?

I have tried to do the below, but it says I can't have that sort of expression for some reason.

ModelState.ToList(item => item.Value.Errors.Count > 0)
Mumble answered 20/5, 2009 at 15:11 Comment(0)
M
7
var errors = from modelstate in ModelState.AsQueryable().Where(f => f.Value.Errors.Count > 0) select new  {  Title = modelstate.Key  };
Mumble answered 22/5, 2009 at 8:38 Comment(0)
E
3

Count is a method. You need ()s after is. But I'd prefer Any, anyway:

from item in ModelState
where item.Value.Errors.Any()
select item.Key
Extinguisher answered 20/5, 2009 at 15:42 Comment(3)
No overload for method 'ToList' takes '1' argumentsMumble
I also just want the keys collection returned not the KeyValuePair collectionMumble
I rewrote your code as LINQ. This is from memory, so I don't guarantee no errors or typos.Extinguisher

© 2022 - 2024 — McMap. All rights reserved.