MVC 5 app....
I have a model with many fields... 2 of them are... (This is the model.)
public string eventStatus {get; set;}
public DateTime? eventDateStarted {get; set;}
public DateTime? eventDateCompleted {get; set;}
What I want to do is never allow eventStatus to be set to "Closed" UNLESS the eventDateStarted and eventDateCompleted fields are not null. If so then result in an invalid ModelState.
PLEASE NOTE: There is programming logic involved here, which could get more advanced based on business rules. So, it's not just simply a matter of saying this field is required in order for this one to be set, etc...
I was thinking that the best place to put this logic is in the set method of eventStatus. Is this a best practice solution? If so, how can I force the ModelState to be invalid?
This is what I've written so far...
[Display(Name = "Status")]
public string eventStatus
{
set
{
if (EventActualEnd is null || EventActualStart is null)
{
ModelState.AddModelError("Status", "Start and End date is mandatory");
}
}
}
But, it doesn't like AddModelError.