Fares Ayyad

Fares Ayyad

  • NA
  • 235
  • 74.1k

MVC: call ajax script after check ModelState in controller?

Mar 7 2017 8:44 AM
Hello if i wnat to call the following script after check the ModelState property in the controller in order to ensure validation, how can i implment this :
 
  1. <script>  
  2.                  $(document).ready(function () {  
  3.   
  4.                      $("#savedata").on("click"function (evt) {  
  5.                          evt.preventDefault();  
  6.                          // var form = $("form");  
  7.                                
  8.                           
  9.                              var data = {  
  10.                                  Name: $("#userid").val(),  
  11.                                  Password: $("#password").val()  
  12.                              }  
  13.                              $.ajax({  
  14.   
  15.                                  url: "/api/login",  
  16.                                  type: "POST",  
  17.                                  data: JSON.stringify(data),  
  18.                                  contentType: "application/json",  
  19.                                  success: function () {  
  20.                                      alert("Success");  
  21.   
  22.                                  },  
  23.                                  error: function () {  
  24.                                      alert("Fail");  
  25.                                  }  
  26.                              });//Ajax call  
  27.   
  28.                            
  29.                          
  30.                      });//savedata  
  31.                       
  32.                  });  
  33.              </script>  
The url is calling an api that will check if the usernam and password matches those in database.
 
the view also contatin beginForm that will be associated to action in controller to check ModelState:
 
  1. @using (Html.BeginForm("LoginProcess""Login"))  
  2. {  
  3.     <div class="container">  
  4.         <div class="row">  
  5.             <div class="col-md-6 col-md-offset-3 alert alert-warning">  
  6.                 <div class="row">  
  7.                     <div class="col-md-12">  
  8.                         <div class="form-group">  
  9.                             @Html.LabelFor(c => c.Customers.Name)  
  10.                             @Html.TextBoxFor(c => c.Customers.Name, new { @class = "form-control" })  
  11.                             @Html.ValidationMessageFor(c => c.Customers.Name)  
  12.                         </div>  
  13.                         <div class="form-group">  
  14.                             @Html.LabelFor(c => c.Customers.Password)  
  15.                             @Html.TextBoxFor(c => c.Customers.Password, new { @class = "form-control" })  
  16.                             @Html.ValidationMessageFor(c => c.Customers.Password)  
  17.                         </div>  
  18.                         @Html.AntiForgeryToken()  
  19.                         <button value="Submit" class="btn btn-primary">Save</button>  
  20.                     </div>  
  21.                 </div>  
  22.             </div>  
  23.         </div>  
  24.     </div>  
  25.   
  26. }  
What i want again is to check ModelState to validate the two textBoxes then if it evaluates to true then run the script.
 
 

Answers (3)