我正在 MVC 4 中编写自定义客户端验证并在 Firebug 中设置断点。这条线有问题:
jQuery.validator.addMethod("greaterthanattrebutedate", function (value, element, param) {
return Date.parse(value) > Date.parse($(param).val());
});
在第一行之后,它不会进入返回行。在第一行 d 是未定义的。
public string OtherPrpertyName { get; set; }
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationType = "greaterthanattrebutedate"; rule.ValidationParameters.Add("other",OtherPrpertyName);
yield return rule;
}
jQuery代码
/// <reference path="jquery-1.6.2-vsdoc.js" />
/// <reference path="jquery.validate-vsdoc.js" />
/// <reference path="jquery.validate.unobtrusive.js" />
/// <reference path="jquery.validate.unobtrusive.min.js" />
$(document).ready(function () {
jQuery.validator.addMethod("greaterthanattrebutedate", function (value, element, param) {
// debugger;
return Date.parse(value) > Date.parse($(param).val());
});
});
jQuery.validator.unobtrusive.adapters.add("greaterthanattrebutedate", ["other"], function (options) {
options.rules["greaterthanattrebutedate"] = "#" + options.params.other;
options.messages["greaterthanattrebutedate"] = options.message;
});