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

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

问题描述

我将 ASP .NET MVC 3 与数据注释和 jQuery 验证插件一起使用.

有没有办法标记某个字段(或某个数据注释)应该只在服务器端进行验证?

我有一个电话号码字段,上面有一个屏蔽插件,正则表达式验证器在用户端发疯了.正则表达式只是一个故障安全(以防有人决定破解 javascript 验证),所以我不需要它在客户端运行.但我仍然希望其他验证运行客户端.

解决方案

我不确定这个解决方案是否适用于 MVC3.它肯定适用于 MVC4:

您可以简单地在呈现字段之前在 Razor 视图中禁用客户端验证,并在呈现字段后重新启用客户端验证.

示例:

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

这里我们禁用了 BatchId 字段的客户端验证.

我还为此开发了一个小帮手:

公共静态类 YnnovaHtmlHelper{公共静态 ClientSideValidationDisabler BeginDisableClientSideValidation(this HtmlHelper html){返回新的 ClientSideValidationDisabler(html);}}公共类 ClientSideValidationDisabler : IDisposable{私人 HtmlHelper _html;公共 ClientSideValidationDisabler(HtmlHelper html){_html = html;_html.EnableClientValidation(false);}公共无效处置(){_html.EnableClientValidation(true);_html = 空;}}

您将按如下方式使用它:

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

如果有人有更好的解决方案,请告诉我!

希望对您有所帮助.

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?

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.

解决方案

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.

Example:

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

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

You will use it as follow:

<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!

Hope this help.

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

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