Good afternoon!
I have a model associated with a JSON request, for example:
public class MyModel { public bool isSomeFeatureEnabled { get; set; } }
And a controller like this:
public ActionResult Submit(MyModel request) { if (ModelState.IsValid) { .. } else { .. } }
When I send an empty JSON object to this action ( {} ), it will be valid with isSomeFeatureEnabled=false . But I want this field to be βrequiredβ in terms that it should always be set to a specific value (true \ false).
I can make this field null and put [Required] on it, but this field is really not valid for every model logic.
Same story with int , double and DateTime fields.
I am using ASP.NET MVC 3 and the default setting is:
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = true
Shouldn't this work for this situation?
source share