ASP .NET MVC禁用客户端验证在每场级别 [英] ASP .NET MVC Disable Client Side Validation at Per-Field Level

查看:347
本文介绍了ASP .NET MVC禁用客户端验证在每场级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP .NET MVC 3数据注释和jQuery的验证插件。

I'm using ASP .NET MVC 3 with Data Annotations and the jQuery validate plugin.

有没有一种方法来标记某一领域(或某些数据注解)只应验证服务器端?

Is there a way to mark that a certain field (or certain data annotation) should only be validated server-side?

我有它屏蔽插件电话号码字段,和普通的前pression验证那张用户端疯了。正则表达式是唯一的故障安全(如果有人决定黑客的JavaScript验证),所以我并不需要它来在客户端上运行。但我还是想其他验证运行客户端。

I have a phone number field with a masking plugin on it, and the regular expression validator goes crazy on the user's end. The regex is only a fail-safe (in case someone decides to hack the javascript validation), so I don't need it to run on the client side. But I'd still like the other validation to run client side.

推荐答案

我不知道这是否解决方案适用于MVC3。它肯定适用于MVC4:

I'm not sure if this solution works on MVC3. It surely works on MVC4:

您可以简单地禁用客户端验证在剃刀呈现场上和场已经呈现后重新启用客户端验证之前查看。

You can simply disable client side validation in the Razor view prior to render the field and re-enable client side validation after the field has been rendered.

例如:

<div class="editor-field">
    @{ Html.EnableClientValidation(false); }
    @Html.TextBoxFor(m => m.BatchId, new { @class = "k-textbox" })
    @{ Html.EnableClientValidation(true); }
</div>

下面,我们禁用客户端验证的BatchId领域。

Here we disable client side validation for the BatchId field.

此外,我已经开发了一个小帮手这样的:

Also I have developed a little helper for this:

public static class YnnovaHtmlHelper
{
    public static ClientSideValidationDisabler BeginDisableClientSideValidation(this HtmlHelper html)
    {
        return new ClientSideValidationDisabler(html);
    }
}

public class ClientSideValidationDisabler : IDisposable
{
    private HtmlHelper _html;

    public ClientSideValidationDisabler(HtmlHelper html)
    {
        _html = html;
        _html.EnableClientValidation(false);
    }

    public void Dispose()
    {
        _html.EnableClientValidation(true);
        _html = null;
    }
}

您将使用它如下:

<div class="editor-field">
    @using (Html.BeginDisableClientSideValidation()) {
        @Html.TextBoxFor(m => m.BatchId, new { @class = "k-textbox" })
    }
</div>

如果任何人有更好的解决方案,请让我知道!

If anyone has better solutions please let me know!

希望这有助于。

这篇关于ASP .NET MVC禁用客户端验证在每场级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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