I have two separate VIEWS accessing the same MODEL. When I put the validator data annotations on the model, it works as advertised and prevents the data from being submitte
This isn't possible via the default set of data annotations.. however, you have the choice of using 2 separate view models or writing your own validationAttribute.
I wrote this once.. although I loathed using it..
public class RequiredOnAttribute : ValidationAttribute
{
public string[] URLs { get; set; }
public override bool IsValid(object value)
{
if (URLs.Contains(System.Web.HttpContext.Current.Request.Url.AbsolutePath))
{
if (string.IsNullOrEmpty(value as string))
{
return false;
}
}
return true;
}
}
Usage:
[RequiredOn(URLs = new string[] { "/create", "/edit" })]
public string MyModelField { get; set; }
You could do the same for Range, RegEx, etc..