自定义验证失败,显示“无法读取未定义的属性'调用'” MVC jQuery

时间:2019-03-06 12:41:06

标签: c# asp.net-mvc unobtrusive-validation

我一生无法解决此脚本的问题。我正在按照本文https://www.codeproject.com/Tips/780992/Asp-Net-MVC-Custom-Compare-Data-annotation-with-Cl

中的说明进行自定义验证

我唯一要更改的是一些日期格式处理。在这里查看我的customcompare.js:

$.validator.addMethod("genericcompare", function (value, element, params) {
// debugger;
var propelename = params.split(",")[0];
var operName = params.split(",")[1];
if (params == undefined || params == null || params.length == 0 ||
    value == undefined || value == null || value.length == 0 ||
    propelename == undefined || propelename == null || propelename.length == 0 ||
    operName == undefined || operName == null || operName.length == 0)
    return true;
var valueOther = $(propelename).val();

if (isNaN(value)) {
    var year1 = value.split('/')[2].substring(0, 4);
    var time1 = value.split('/')[2].substring(5, 999);
    var hour1 = time1.split(':')[0];
    var minute1 = time1.split(':')[1];

    var test1 = new Date(year1, value.split('/')[1] - 1, value.split('/')[0], hour1, minute1);
    val1 = Date.parse(test1);
}
else {
    val1 = eval(value);
}

if (isNaN(valueOther)) {
    var year2 = valueOther.split('/')[2].substring(0, 4);
    var time2 = valueOther.split('/')[2].substring(5, 999);
    var hour2 = time2.split(':')[0];
    var minute2 = time2.split(':')[1];

    var test2 = new Date(year2, valueOther.split('/')[1] - 1, valueOther.split('/')[0], hour2, minute2);
    val2= Date.parse(test2);
}
else {
    val2 = eval(valueOther);
}


if (operName == "GreaterThan")
    return val1 > val2;
if (operName == "LessThan")
    return val1 < val2;
if (operName == "GreaterThanOrEqual")
    return val1 >= val2;
if (operName == "LessThanOrEqual")
    return val1 <= val2; });
$.validator.unobtrusive.adapters.add("genericcompare",
    ["comparetopropertyname", "operatorname"], function (options) {
        options.rules["genericcompare"] = "#" +
            options.params.comparetopropertyname + "," + options.params.operatorname;
        options.messages["genericcompare"] = options.message;
    });

我得到的错误是:“未捕获的TypeError:无法读取未定义的属性'call'。检查元素LaycanStartDate,检查'genericcompare'方法时发生异常”

型号:

[Display(Name = "LaycanStartDate", ResourceType = typeof(Resources.Resources))]
    [DataType(DataType.DateTime)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy hh:mm}")]
    [GenericCompare(CompareToPropertyName = "LaycanEndDate", OperatorName = GenericCompareOperator.LessThanOrEqual, ErrorMessageResourceName = "LaycanStartBeforeEnd",ErrorMessageResourceType = typeof(Resources.Resources))]
    public DateTime LaycanStartDate { get; set; }

1 个答案:

答案 0 :(得分:0)

您正在将自定义JQuery Validation逻辑与Unobtrusive Validation脚本一起使用。禁用相关页面,不会出现问题。