ASP.NET MVC3 - 自定义验证属​​性 - >客户端破 [英] ASP.NET MVC3 - Custom validation attribute -> Client-side broken

查看:100
本文介绍了ASP.NET MVC3 - 自定义验证属​​性 - >客户端破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现与客户端验证过自定义验证属​​性。

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

我的属性如下:

public class FileSize : ValidationAttribute, IClientValidatable
{
    private readonly int _size;

    public FileSize(int size)
    {
        ErrorMessage = "Invalid size.";
        _size = size;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ValidationType = "fileSize",
            ErrorMessage = ErrorMessage
        };

        rule.ValidationParameters["size"] = _size;

        yield return rule;
    }

    public override bool IsValid(object value)
    {
        return ((HttpPostedFileBase) value).ContentLength < _size;
    }
}

和脚本包括在我看来是这样的:

and the script includes in my view looks like:

<script src="@Url.Content("~/Scripts/jquery-1.4.4.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        $.validator.unobtrusive.adapters.addSingleVal('fileSize', 'size');
    });
</script>

问题是,即使所有这些脚本包括并注册一个新的适配器的功能,然后在客户端验证仍然没有工作,它只是简单地继续使用只有服务器端验证...

The problem is that even with all of those script includes and the function that registers a new adapter, then the client-side validation is still not working, it simply just keep using the server-side validation only...

任何想法?

推荐答案

您缺少一些JavaScript。

You are missing some JavaScript.

布拉德威尔逊大于例如,从他的的href=\"http://bradwilson.typepad.com/blog/talks.html\">高级ASP.NET MVC 3 谈话。

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

这是它的一些源$ C ​​$ C:

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));

这篇关于ASP.NET MVC3 - 自定义验证属​​性 - &GT;客户端破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆