ASP.Net MVC에서 성가신 부족 중 하나는 확인란 유효성 검사입니다. 눈에 거슬리지 않는 유효성 검사는 체크 박스에서 작동하지 않습니다. 다른 솔루션을 참조하십시오
public class EnforceTrueAttribute : ValidationAttribute, IClientValidatable
{
public override bool IsValid(object value)
{
if (value == null) return false;
if (value.GetType() != typeof(bool)) throw new InvalidOperationException("can only be used on boolean properties.");
return (bool)value == true;
}
public override string FormatErrorMessage(string name)
{
return "The " + name + " field must be checked in order to continue.";
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule
{
ErrorMessage = String.IsNullOrEmpty(ErrorMessage) ? FormatErrorMessage(metadata.DisplayName) : ErrorMessage,
ValidationType = "enforcetrue"
};
}
}
그리고 내 JS에서 :
ASP.NET MVC CheckBox Validations (server and client side)
MVC unobtrusive validation on checkbox not working
당신은 일 단 하나 나를 위해. 고마워요. 많은 사람. – user3754372