using
System;
using
System.ComponentModel.DataAnnotations;
namespace WebApplication1.Models
{
public class Home
{
[Required]
public string Prop1 { get; set; }
[CustomRequired("Prop1",
ErrorMessage = "The
Prop2 field is required.")]
public string Prop2 { get; set; }
}
public class CustomRequiredAttribute : ValidationAttribute
{
string prop1 = string.Empty;
public CustomRequiredAttribute(string prop1)
{
this.prop1 =
prop1;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
object strProp1 = GetPropValue(validationContext.ObjectInstance, prop1);
if (Convert.ToString(strProp1).ToLower() != "admin")
{
return new ValidationResult("");
}
return ValidationResult.Success;
}
public static object
GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}
}
}