客户端验证定制标注Asp.Net MVC 4 [英] Client side validation for custom annotation Asp.Net MVC 4

查看:172
本文介绍了客户端验证定制标注Asp.Net MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指这篇文章:

<一个href="http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx">http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

这显示了如何在Asp.Net MVC 2.创建自定义的注解然而,在客户端验证脚本,特别是MicrosoftMvcJQueryValidation不是Asp.Net MVC4可用。我读了它在一篇文章是Asp.Net期货项目的一部分。我想用jQuery来连接我的客户端验证。在我的项目模板脚本的文​​件夹中,我看到一个名为脚本:

which shows how to create custom annotation in Asp.Net MVC 2. However, the client side validation scripts, especially "MicrosoftMvcJQueryValidation" is not available in Asp.Net MVC4. I read it on one article it is part of Asp.Net Futures project. I want to hook up my client side validation using Jquery. In my project template script's folder, I see scripts named:

jquery.validate.min.js
jquery.validate.unobtrusive.min.js
jquery.unobtrusive-ajax.min.js

有什么办法,我可以利用这些现有的脚本?或者我必须强制下载期货的项目?

Is there any way I can make use of these existing scripts? or do I have to compulsorily download futures project?

推荐答案

这条是针对MVC 2的使用MicrosoftAjax。 MVC 4不再包括该MS的Ajax文件,因为它们已经德precated和preferred方法是使用jquery

That article is specific to MVC 2 which used MicrosoftAjax. MVC 4 no longer includes the MS Ajax files as they have been deprecated and the preferred method is to use jquery.

要验证您的设置,确保这些脚本是在你的布局

To verify your settings, make sure these scripts are in your layout

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

和这两个设置是present在appSettings部分在你的web.config文件

And these two settings are present in the appSettings section in your web.config file

<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

所以,当你将数据添加注释到您的ViewModels你的客户端和服务器端验证这两个

So when you add data annotations to your ViewModels you get client side and server side validation both

public class MyModel 
{
    [Required]
    [StringLength(30)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(30)]
    public string LastName { get; set; }
}

在你看来只是确保你有code这样

In your view just make sure you have code like this

<div>
    @Html.LabelFor(model => model.FirstName)
</div>
<div>
    @Html.TextBoxFor(model => model.FirstName) <br/>
    @Html.ValidationMessageFor(model => model.FirstName)
</div>

<div>
    @Html.LabelFor(model => model.LastName)
</div>
<div>
    @Html.TextBoxFor(model => model.LastName) <br/>
    @Html.ValidationMessageFor(model => model.LastName)
</div>

更新

下面就是我呼吁RateRequiredIfCustomIndexRate自定义验证程序的示例 这是它的JavaScript端,以便它被添加到jQuery验证

Here's an example of a custom validator that I have called RateRequiredIfCustomIndexRate This is the javascript side of it so that it gets added to jquery validation

$("document").ready(function () {

    var isCustomRateRequired = document.getElementById("IsCustomRateRequired");

    isCustomRateRequired.onchange = function () {
        if (this.checked) {
            $('#Rate').attr('disabled', 'disabled');
            $('#Rate').val('');
        }
        else {
            $('#Rate').removeAttr('disabled');
        }
    };
});

jQuery.validator.addMethod("raterequiredifcustomindexrate", function (value, element, param) {
    var rateRequired = $("#CustomRateRequired").val();
    if (rateRequired && value == "") {
        return false;
    }
    return true;
});

jQuery.validator.unobtrusive.adapters.addBool("raterequiredifcustomindexrate");

这篇关于客户端验证定制标注Asp.Net MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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