클라이언트 유효성 검사 문제 (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"));
}
이것은 아마도 문제가되지 않지만 유효성 검사는 항상 false를 반환합니다. –
오타되었습니다. 방금 코드를 업데이트했습니다. addMethod에서 null 오류가 발생하는 이유를 알고 계십니까? – rk1962
새로운 MVC 4/RC 인터넷 앱을 만들고 F12 도구를 사용하여 계정 컨트롤러/뷰 (등록, 비밀번호 변경 등)의 유효성을 이해하십시오. 그렇게하면 jQuery가로드되지 않는 이유를 파악할 수 있습니다. – RickAndMSFT