title: MVC Ajax BeginForm validation
body: <p>I have a Bootstrap modal that makes an insert. I am using Ajax BeginForm and I have problem with the validation. Normal validation like: required fields works great. My problem is that I have a server validation and when it goes into the controller and does not pass my custom validation I tried to return the partialview with the model or even a json but I am not able to trigger the validation on the modal. I googled a lot and I was not able to solve my problem.</p>
  
  <p>Here is the code</p>
  
  <pre><code>public class ContactDto
  {
      public decimal IdContact{ get; set; }
      public decimal? CounterpartyId { get; set; }
      public string Name { get; set; }
      public string Address { get; set; }
      public string City { get; set; }   
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public JsonResult EditContact(ContactDto contact)
  {
      if (CustomValidation(code) &amp;&amp; ModelState.IsValid)
      {
          _dbManager.CreateCode(code);
  
          //return RedirectToAction("Details", "Counterparties", new { id = code.CodeCounterpartyId });
          return Json(new { Data = "Ok" }, JsonRequestBehavior.AllowGet);
      }
  
      return Json(new { Data = "Nok" }, JsonRequestBehavior.AllowGet);
  }
  </code></pre>
  
  <p>Modal with Ajax:</p>
  
  <pre><code>@using System.Globalization
  @model UpdateTabs.Models.ContactDto
  
  &lt;div id="EditContactModalDiv" class="modal fade" role="dialog"&gt;
      &lt;div class="modal-dialog modal-lg"&gt;
  
      .......
  
          @using (Ajax.BeginForm("EditContact", "Home", new AjaxOptions { HttpMethod = "POST", OnSuccess = "ValidateKeys"}, new { id = "formCreateContact" }))
          {
              @Html.AntiForgeryToken()
              @Html.ValidationSummary(false, "", new { @class = "text-danger" })
  
              ......
  
              @Html.TextBoxFor(m =&gt; m.Name)
              &lt;br/&gt;
              @Html.ValidationMessageFor(m =&gt; m.Name)
  
              ......      
          }   
  
      .......
  
      &lt;/div&gt;
  &lt;/div&gt;      
  </code></pre>
  
  <p>and the Jquery part:</p>
  
  <pre><code>function ValidateKeys(result) {
      if (result.Ok) {
          $('#CreateCodeModalDiv').modal('hide');
      } else {
  
          alert("Model is invalid !");
          $('#CreateCodeModalDiv').modal('show');
      }
  }
  </code></pre>
  
  <p>and until here is working, but after here I don't know what to do to trigger the validation message for.</p>
  
  <p>Any help please?
  Thanks</p>