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

时间:2010-04-06 15:35:06

标签: jquery ajax validation

我创建了一个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>

因为我使用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();
                }
            });
        });
    });

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

谢谢, 穆罕默德

1 个答案:

答案 0 :(得分:0)

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

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

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