I have a class that contains a Dictionary<int, object>
property that is marked as required. When I deserialize the data coming into the controller onto that class, the Required
attribute works to prevent null
s from entering that property, but it does not stop null
s from entering the dictionary as a value given the key value pairs are formatted and passed in correctly.
Is there a way to have the Required
attribute also prevent null
s from being values in the dictionary? Or is there another attribute that can be added to that property to accomplish this?
Or would the best way to solve this be to roll my own class that essentially consists of key-value pairs that I can mark both the key property and value property as Required
? EX:
public class Example
{
[Required]
public int Key;
[Required]
public object Value;
}
And then just have an IEnumerable<Example>
instead of Dictionary<int, object>
?