Search code examples
asp.net-mvcxval

ajax.beginform and xVal


I am using Ajax.BeginForm to submit a form ans xVal to enforce validation

The problem the ajax post request is being made even when the form has errors on it. I know client-side validation is working, because it is displaying proper error messages and what not, but why it thinks it is okay to make the ajax request anyway is something I don't understand.

I even tried putting causesvalidation = true inside the submit tab, but the form is still being submitted.

    <% using (Ajax.BeginForm("SuggestTemp", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "Result" }))
       {%>

        <table>
         <tr>
        <td style="text-align:right">
            Title
        </td>
        <td>
            <%= Html.TextBox("Upload.Title")%>
            <%= Html.V
alidationMessage("Upload.Title")%>
        </td>
        </tr> <tr><td><%=Html.ClientSideValidation<Upload>("Upload") %></td><td></td></tr>
        <tr><td></td><td><input type = "submit" causesvalidation = "true"  value = "Suggest " class = "btn"/></td></tr> 
      </table>
      <div id = "Result"><%=ViewData["SuggestStatus"]%></div> 

Solution

  • try to put $("#yourFormId").validate() to the OnBegin parameter

    Ex:

    <%
    using (Ajax.BeginForm("SuggestTemp", "yourControllerName", new{}, new AjaxOptions
    {
        HttpMethod = "POST",
        OnBegin = "function(){$('#myForm').validate();}"
    },
    new { id = "myForm" }){ %>