2014-06-18 1 views
1

데이터 주석이있는 필드가있는 양식이 있는데 데이터 주석이있는 "Accept Terms and Conditions"체크 박스가 있습니다. [Range(typeof(bool), "true", "true", ErrorMessage="You must accept Terms And Conditions to proceed")] 모든 것이 잘 작동하지만 성가신 것은 Required 데이터 주석이 오류를 발생시키는 것입니다 게시하기 전에 .. 내 말은, 나는 submit을 클릭하고 폼은 포스트를 만들지 않고 에러를 보여 주지만, Range, form post 그리고 에러를 보여준다.ASP.NET MVC : 클라이언트 측의 Range Data Annotation이 작동하지 않는 이유는 무엇입니까?

왜 그럴까요? 그게 공통된 행동인가요?

답변

1

내가 마지막으로이 솔루션과 함께 제공

jQuery.validator.addMethod("enforcetrue", function (value, element, param) { 
      return element.checked; 
     }); 
     jQuery.validator.unobtrusive.adapters.addBool("enforcetrue"); 
+0

당신은 일 단 하나 나를 위해. 고마워요. 많은 사람. – user3754372

0

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