If I have an MVC model that (grossly simplified) looks like this;
public class Person
{
[Required]
public string SpecialSauce { get; set; }
[Required]
public string Name { get; set; }
}
However, only the Name
comes from the view. The SpecialSauce
is provided server side.
person.SpecialSauce = "Ketchup"; //Hard-coded for example
However, before I save, I check ModelState.IsValid
, which returns false, with the error "The SpecialSauce field is required."
How do I make the ModelState valid when the required model property is provided server side? I could remove the [Required]
data annotation, but I want the EF database column to be non-nullable.