I have a column that is Required in my model
[Required(ErrorMessage = "The Name field is required")]
public string Name{ get; set; }
However, is it only required on different conditions.
So I remove the ModelState
Key when the correct condition is met
if (Status_ID != 2 && Status_ID != 3)
{
ModelState.Remove("Name");
}
Which works, but when EF tries to save the entity, I get a EntityVaildationError
because I am guessing I have the Data Annotation
"Required" on the property which can never been taken off programmatically regardless of the ModelState
How else could I achieve what I want?
Cheers