2012-07-16 2 views
1

클라이언트 유효성 검사 문제 (MVC4 RC, VS2010)를 도울 수있는 사람이 있습니까? Intellisense는 jQuery.validator에 addMethod를 표시하지 않습니다. 응용 프로그램을 실행할 때이 메서드는 javascript null 오류가 발생합니다. 사용자가 현재 시간보다 큰 시간을 입력 할 수있게하고 싶습니다. 내 CustomValidation.js 파일은 다음과 같다 : 아래 그림과 같이 내가 BundleConfig.cs 파일에 CustomValidation.js을 추가 한클라이언트 검증 문제 - MVC 4 RC

public class DateGreaterThanAttribute : ValidationAttribute, IClientValidatable 
    { 
     private const string DefaultErrorMessage = "{0} Time must be greater than current time"; 

     public DateGreaterThanAttribute() : 
      base(DefaultErrorMessage) 
     { } 

     protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
     { 
      ValidationResult validationResult = ValidationResult.Success; 

      if (((DateTime)value).TimeOfDay >= DateTime.Now.TimeOfDay) 
       return validationResult; 
      else 
      { 
       return new ValidationResult(FormatErrorMessage(this.ErrorMessage));     
      } 
     } 

     public override string FormatErrorMessage(string name) 
     { 
      return base.FormatErrorMessage(name); 
     } 


     #region IClientValidatable Members 

     public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) 
     { 
      var rule = new ModelClientValidationRule(); 
      rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()); 
      rule.ValidationType = "dategreaterthan"; 
      yield return rule;     
     } 

     #endregion 
    } 

: 여기

/// <reference path="jquery-1.7.2-vsdoc.js" /> 
/// <reference path="jquery.validate-vsdoc.js" /> 
/// <reference path="jquery.validate.unobtrusive.js" /> 

jQuery.validator.addMethod('dategreaterthan', function (value, element, param) { 
     var today = new Date(); 
     return Date.parse(value) > today; 
}); 

$.validator.unobtrusive.adapters.add("dategreaterthan", function (options) { 
    options.rules["dategreaterthan"] = true; 
    options.messages["dategreaterthan"] = options.message; 
}); 

사용자 지정 유효성 검사 코드를 속성 공개 클래스 BundleConfig (가 맞는지 확실하지) :

public static void RegisterBundles(BundleCollection bundles) 
    { 
     bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
        "~/Scripts/jquery-1.*")); 

     bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
        "~/Scripts/jquery-ui*")); 

     bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
        "~/Scripts/jquery.unobtrusive*", 
        "~/Scripts/jquery.validate*")); 

     bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
        "~/Scripts/modernizr-*")); 

     bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css")); 

     bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
        "~/Content/themes/base/jquery.ui.core.css", 
        "~/Content/themes/base/jquery.ui.resizable.css", 
        "~/Content/themes/base/jquery.ui.selectable.css", 
        "~/Content/themes/base/jquery.ui.accordion.css", 
        "~/Content/themes/base/jquery.ui.autocomplete.css", 
        "~/Content/themes/base/jquery.ui.button.css", 
        "~/Content/themes/base/jquery.ui.dialog.css", 
        "~/Content/themes/base/jquery.ui.slider.css", 
        "~/Content/themes/base/jquery.ui.tabs.css", 
        "~/Content/themes/base/jquery.ui.datepicker.css", 
        "~/Content/themes/base/jquery.ui.progressbar.css", 
        "~/Content/themes/base/jquery.ui.theme.css")); 
     bundles.Add(new ScriptBundle("~/bundles/customvalidation").Include(
        "~/Scripts/CustomValidation.js"));  
    } 
+0

이것은 아마도 문제가되지 않지만 유효성 검사는 항상 false를 반환합니다. –

+0

오타되었습니다. 방금 코드를 업데이트했습니다. addMethod에서 null 오류가 발생하는 이유를 알고 계십니까? – rk1962

+0

새로운 MVC 4/RC 인터넷 앱을 만들고 F12 도구를 사용하여 계정 컨트롤러/뷰 (등록, 비밀번호 변경 등)의 유효성을 이해하십시오. 그렇게하면 jQuery가로드되지 않는 이유를 파악할 수 있습니다. – RickAndMSFT

답변

2

가 있는지 확인하십시오 당신이,447를 포함하는 jqueryval 번들 (포함했다고및 ~/Scripts/jquery.validate* 스크립트)를 사용자 지정 번들 (사용자가 jqueryui과 연결되어 있음) 앞에 붙여 넣으십시오. 따라서 순서대로 :

@Scripts.Render("~/bundles/jquery") 
@Scripts.Render("~/bundles/jqueryval") 
@Scripts.Render("~/bundles/jqueryui") 
+0

BundleConfig.cs 및 _Layout.cstml을 모두 업데이트했습니다. 'Scripts.Render ("~/번들/JQuery와") Scripts.Render ("~/번들/jqueryval") Scripts.Render ("~/번들/jqueryui") Scripts.Render @ ("@ @ @ ~/bundles/customvalidation ") @RenderSection ("scripts ", required : false)' – rk1962

+0

죄송합니다. 재현 할 수 없습니다. 이것은 나를 위해 작동합니다. 샘플 프로젝트를 보내 주시겠습니까? –

+0

디버거를 사용하여로드 된 스크립트를 확인하십시오. – RickAndMSFT