ASP.NET MVC3 - Custom validation attribute -> Client-side broken

前端 未结 1 1868
野性不改
野性不改 2021-01-12 06:00

I\'m trying to implement a custom validation attribute with client-side validation too.

My attribute looks like:

public class FileSize : ValidationAt         


        
1条回答
  •  清酒与你
    2021-01-12 06:47

    You are missing some JavaScript.

    Look at Brad Wilsons "greater" example from his Advanced ASP.NET MVC 3 talk.

    Some sourcecode from it:

    (function ($) {
    $.validator.addMethod("jqgreater", function (value, element, params) {
        var thisValue, otherValue;
        if (this.optional(element)) { // No value is always valid
            return true;
        }
    
        thisValue = parseInt(value);
        otherValue = parseInt($(params).val());
        return thisValue > otherValue;
    });
    
    function getModelPrefix(fieldName) {
        return fieldName.substr(0, fieldName.lastIndexOf(".") + 1);
    }
    
    function appendModelPrefix(value, prefix) {
        if (value.indexOf("*.") === 0) {
            value = value.replace("*.", prefix);
        }
        return value;
    }
    
    $.validator.unobtrusive.adapters.add("greater", ["other"], function (options) {
        var prefix = getModelPrefix(options.element.name),
            other = options.params.other,
            fullOtherName = appendModelPrefix(other, prefix),
            element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];
    
        options.rules["jqgreater"] = element;  // element becomes "params" in callback
        if (options.message) {
            options.messages["jqgreater"] = options.message;
        }
    });
    } (jQuery));
    

    0 讨论(0)
提交回复
热议问题