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

问题描述 投票:0回答:1

我不能为我的生活找出这个脚本的问题。我正在做这个帖子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:无法读取未定义的属性'调用'。检查元素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; }
c# asp.net-mvc unobtrusive-validation
1个回答
0
投票

您正在使用具有Unobtrusive Validation脚本的自定义JQuery验证逻辑。禁用不显眼的相关页面,问题将得到解决。

© www.soinside.com 2019 - 2024. All rights reserved.