title: Ajax.BeginForm Handling two different onSuccess Response, MVC 5,C#
body: <p>I am new to Ajax and Jquery so i need you to forgive me if i am trying to do something stupid, 
   i am working with MVC 5 and Ajax.Beginform,
  and what i am trying to do is i have a ajax form i need to validate it with jquery unobtrusive,if i am getting it right the Jquery validation work with ModelState and will return the view back again if is a validation error found, in this case i need to update my form so the validation message appears to the user browser ,for example here is my controller :</p>
  
  <pre><code>[HttpPost]
  public ActionResult Index(AddProduct model)
  {
      if (ModelState.IsValid)
      {
          // connect to the database save data etc... 
          return PartialView("~/Views/Shared/_MyModal.cshtml");
      }
      else
      {
          return View(model);
      }
  }
  </code></pre>
  
  <p>if the ModelState.IsValid i should save the data and return partial view (Bootstrap Modal) indicating that the data has been saved successfully,
   else it will return the whole view to display the validation messages and to do that i have to put the TargetId of the ajax form to wrap up the whole ajax form to updated,
  and here the ajax form :</p>
  
  <pre><code>&lt;div id="result"&gt;
   @using (Ajax.BeginForm("Index", new AjaxOptions
    {
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "result",
        HttpMethod = "POST",
        OnBegin = "onBegin();",
        OnComplete = "onCompleated();",
        OnSuccess = "onSuccess()",
        OnFailure = "onFailure()"
   }))
   {
  @Html.ValidationSummary(true)
  &lt;div id="form1" class="form-horizontal"&gt;
      &lt;div class="row"&gt;
          &lt;div class="form-group"&gt;
              @Html.LabelFor(m =&gt; m.Name, new { @class = "col-md-2 control-label" })
              &lt;div class="col-md-10"&gt;
                  @Html.TextBoxFor(m =&gt; m.Name, new { @class = "form-control"       })
                  @Html.ValidationMessageFor(m =&gt; m.Name, String.Empty, new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              @Html.LabelFor(m =&gt; m.Price, new { @class = "col-md-2 control-label" })
              &lt;div class="col-md-10"&gt;
                  @Html.TextBoxFor(m =&gt; m.Price, new { @class = "form-control" })
                  @Html.ValidationMessageFor(m =&gt; m.Price, String.Empty, new { @class = "form-help form-help-msg text-red" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              &lt;button class="btn btn-default col-md-2 col-md-offset-2" type="submit"&gt;Save &amp;nbsp;&amp;nbsp;&lt;/button&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
   }
    &lt;/div&gt;
  </code></pre>
  
  <p>else i will return a partial view and display it as Bootstrap Modal and i need in this case not to update the ajax form targetId i need to keep it intact and just Displaying the Modal, but the problem is in both situation ModelState.IsValid or Is not Valid all response fires onSuccess Method in ajax form i didn't know these is normal or not, and here is the javascript onsuccess() Method : </p>
  
  <pre><code>function onSuccess() {
      $('#myModal').modal('hide')
      $("#resultModal").modal({
          backdrop: 'static',
          keyboard: false
      });
      $('#resultModal').on('hidden.bs.modal', function (e) {
          window.location = "/product";
      });
  }
  </code></pre>
  
  <p>$(#'myModal').modal('hide') is a progress Modal i hide it after the posting has finish,and the next step is i show result Modal which i had return it if the Posting execute fine without validation error,
  the Problem is :</p>
  
  <ul>
  <li><p>i get to update the form using the UpdatetargetId in the ajax form just fine if validation Error happen to be Exist, but that's happen any way if i am returning Validation Error Or partial view that caring the Modal for me,in both situation the form disappear,and that's something i don't wont to do,
  i need to keep the form in case the return Content was partial view and updated in case ModelStat validation Error, maybe i miss understand everything but if i am, i need some explanation to put me back on track .</p></li>
  <li><p>I did an ugly code and messy and i didn't like in onSuccess() Mehtod as mentioned above, i used event fires when i close my result Modal redirect me again to Index so the form Display again but its not clean and i don't like it, i need something professional if exist.</p></li>
  </ul>
  
  <p>Thank you in Advance,</p>