如何清除MVC客户端验证错误时,当用户已经失效形式被点击取消按钮? [英] How do I clear MVC client side validation errors when a cancel button is clicked when a user has invalidated a form?

查看:160
本文介绍了如何清除MVC客户端验证错误时,当用户已经失效形式被点击取消按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主视图中渲染的局部视图。局部视图利用了 System.ComponentModel.DataAnnotations Html.EnableClientValidation()

I have a partial view that is rendered within a main view. The partial view takes advantage of System.ComponentModel.DataAnnotations and Html.EnableClientValidation().

点击一个链接,并在显示包含局部视图的div一个 JQuery.Dialog()

A link is clicked, and div containing the partial view is displayed within a JQuery.Dialog().

我然后点击保存按钮,而不在我的验证的输入字段中输入任何文字。这会导致客户端验证火如预期,而无效的字段旁边显示*为必填的消息。

I then click the save button without entering any text in my validated input field. This causes the client side validation to fire as expected, and display the '*required' message beside the invalid field.

当点击取消按钮,我想在客户端验证MVC重置回它的默认状态并删除任何消息,准备当用户再次打开该对话框。是否有这样做的推荐的方法?

When the cancel button is clicked, I want to reset the client side MVC validation back to it's default state and remove any messages, ready for when the user opens the dialog again. Is there a recommended way of doing this?

推荐答案

如果你只是想清除验证的消息,使它们不会显示给用户,你可以像这样的javascript做到这一点:

If you just want to clear the validation-messages so that they are not shown to the user you can do it with javascript like so:

function resetValidation() {
        //Removes validation from input-fields
        $('.input-validation-error').addClass('input-validation-valid');
        $('.input-validation-error').removeClass('input-validation-error');
        //Removes validation message after input-fields
        $('.field-validation-error').addClass('field-validation-valid');
        $('.field-validation-error').removeClass('field-validation-error');
        //Removes validation summary 
        $('.validation-summary-errors').addClass('validation-summary-valid');
        $('.validation-summary-errors').removeClass('validation-summary-errors');

    }

如果您需要重置只工作你弹出,你可以做这样的:

If you need the reset to only work in your popup you can do it like this:

function resetValidation() {
        //Removes validation from input-fields
        $('#POPUPID .input-validation-error').addClass('input-validation-valid');
        $('#POPUPID .input-validation-error').removeClass('input-validation-error');
        //Removes validation message after input-fields
        $('#POPUPID .field-validation-error').addClass('field-validation-valid');
        $('#POPUPID .field-validation-error').removeClass('field-validation-error');
        //Removes validation summary 
        $('#POPUPID .validation-summary-errors').addClass('validation-summary-valid');
        $('#POPUPID .validation-summary-errors').removeClass('validation-summary-errors');

    }

我希望这是你所寻求的效果。

I hope this is the effect you seek.

这篇关于如何清除MVC客户端验证错误时,当用户已经失效形式被点击取消按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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