在向服务器发送jQuery Ajax调用之前访问Microsoft Ajax验证对象

时间:2022-08-23 17:56:07

I've created a View which I do both server-side and client-side validation on the page using DataAnnotation attributes which decorate each input on the form. The validation take place when user tab out from a form control or when user clicks on "Submit" button which is of type "submit".

我创建了一个View,我使用DataAnnotation属性在页面上进行服务器端和客户端验证,这些属性用于装饰表单上的每个输入。当用户从表单控件中选项卡或用户单击“提交”类型的“提交”按钮时,将进行验证。

        <table width="100%">
            <tr>
                <td class="editor-label" width="20%">
                    <%= Html.LabelFor(model => model.From)%>
                    (Email)*
                </td>
                <td width="80%">
                    <%= Html.TextBoxFor(model => model.From, new { size=30, maxlength=100})%>
                    <%= Html.ValidationMessageFor(model => model.From)%>
                </td>
            </tr>
       <tr>
                <td class="editor-label" width="20%">
                </td>
                <td width="80%">
                    <input type="button" id="submitFeedback" value="Send" />
                    <input type="button" id="cancelFeedback" value="Cancel" onclick="closePopup();" />
                </td>
            </tr>

Since I'm posting form data through Async Ajax call using jQuery. It bypasses the validation set on the form. I want the validation to occur when when user clicks on the button that I use in place of "submit" button.

因为我使用jQuery通过Async Ajax调用发布表单数据。它绕过表单上的验证集。我希望在用户点击我使用的按钮代替“提交”按钮时进行验证。

    $(function() {
        $('#submitFeedback').click(function() {
            $.ajax({
                type: "POST",
                url: "/Feedback/SubmitFeedback",
                data: { from: $('#From').val(), to: $('#To').val(), subject: $('#Subject').val(), body: $('#Comments').val() },
                success: function(feedback) {
                    alert(feedback);
                    closePopup();
                }
            });
        });
    });

I need to have access the Validation object of Microsoft Ajax library on the client side and I assume it has to have some "IsValid" method.

我需要在客户端访问Microsoft Ajax库的Validation对象,我认为它必须有一些“IsValid”方法。

Thanks, Mohammad

谢谢,*

1 个解决方案

#1


0  

Set the name of your javascript validation function in the Ajax.BeginForm() AjaxOptions:

在Ajax.BeginForm()AjaxOptions中设置javascript验证函数的名称:

Ajax.BeginForm("ActionMethod", new AjaxOptions(){OnBegin = "validateForm"}) { ...

This will call the validation function before the form is submitted.

这将在提交表单之前调用验证函数。

#1


0  

Set the name of your javascript validation function in the Ajax.BeginForm() AjaxOptions:

在Ajax.BeginForm()AjaxOptions中设置javascript验证函数的名称:

Ajax.BeginForm("ActionMethod", new AjaxOptions(){OnBegin = "validateForm"}) { ...

This will call the validation function before the form is submitted.

这将在提交表单之前调用验证函数。