I use ASP.NET MVC4 in my solution. I have the ViewModel below where I would like to validate that the field EmergencyReason is filled only if the field Date is today. I try this:
public class LoadingViewModel
{
public DateTime Date { get; set; }
[RequiredIf("Date", Comparison.IsEqualTo, DateTime.Today)]
public string EmergencyReason { get; set; }
...
}
It doesn’t work. The third argument of RequiredIf must be a constant expression, …
Any idea how can I force the user to enter an EmergencyReason only if Date field is today?
Thanks.
You seem to be using some non-standard
RequiredIfattribute which is not part of the standard ASP.NET MVC 4 package.As you know C# allows you to only pass constant values to attributes. So one possibility is to write a custom attribute:
and then: