1
Answer

Pass the value of model into controller using ajax?

im trying to pass the value of model into controller but my model is nulll but my other parameter in controller had a value like tracking, deposit, id. what is the problem in my code. im using mvc 5 asp.net.Thanks
 
My Controller 
 
  1.  public ActionResult AddEmptyBox(EmptyBoxFormModel model, string[] tracking, string[] deposit, int id)  
  1.  {  
  2.   return View(model);
  3.  }  
  1.   
 My View
 
  1. <div class="ContainerBanner">  
  2.     <br />  
  3.     <div class="row">  
  4.         <form id="order-frm" action="">  
  5.             @Html.TextBoxFor(x => x.Id, new { @id = "hid-order-id" })  
  6.             @Html.Raw(TempData["msg"])  
  7.   
  8.             @Html.Partial("_Message")  
  9.   
  10.             <div class="row">  
  11.                 <div class="col-md-2">  
  12.                     @Html.LabelFor(x => x.Quantity, new { @class = "form-label" })  
  13.                     @Html.TextBoxFor(x => x.Quantity, new { @class = "form-control", @placeholder = "Tracking number", @onblur = "displayTextBoxes()", @id = "text-count" })  
  14.                 div>  
  15.   
  16.                 <div class="col-md-2">  
  17.                     <div class="form-group">  
  18.                         @Html.LabelFor(x => x.DateDelivered, new { @class = "form-label" })  
  19.                         <div class='input-group date' id='datetimepicker1'>  
  20.                             @Html.TextBoxFor(x => x.DateDelivered, new { @class = "form-control" })  
  21.                             <span class="input-group-addon">  
  22.                                 <span class="glyphicon glyphicon-calendar">span>  
  23.                             span>  
  24.                         div>  
  25.                     div>  
  26.                 div>  
  27.             div>  
  28.   
  29.             <br />  
  30.             <div class="row">  
  31.                 <div class="col-md-12">  
  32.                     <div id="testing-container">div>  
  33.                 div>  
  34.             div>  
  35.   
  36.             <br />  
  37.             <button type="submit" id="btnSubmit" class="btn btn-primary btn-sm"><i class="fas fa-save">i>  Save Emptyboxbutton>  
  38.             <a href="@Url.Action("emptyboxes", "order")" class="btn btn-default btn-sm"><i class="fa fa-long-arrow-alt-left">i>  Back to lista>  
  39.         form>  
  40.     div>  
  1. function displayTextBoxes() {  
  2.         var count = $('#text-count').val();  
  3.   
  4.         var tracking = '';  
  5.         var deposit = '';  
  6.   
  7.         for (i = 1; i <= count; i++)  
  8.         {  
  9.             $('#testing-container').append('' + tracking + deposit + '');  
  10.         }  
  11.     }  
  12.   
  13.     var tracking = [];  
  14.     var deposit = [];  
  15.   
  16.     function getAllData() {  
  17.         $('#testing-container').each(function () {  
  18.             var t = $(this).find('.tracking').val();  
  19.             var d = $(this).find('.deposit').val();  
  20.   
  21.             tracking.push(t);  
  22.             deposit.push(d);  
  23.         });  
  24.     }  
  25.   
  26.     $('#btnSubmit').click(function () {  
  27.   
  28.         getAllData();  
  29.         var order = $('#order-frm').serialize();  
  30.         var idOrder = $('#hid-order-id').val();  
  31.   
  32.         $.ajax({  
  33.             type: 'POST',  
  34.             url: '@Url.Action("addemptybox", "order")',  
  35.             contentType: "application/json; charset=utf-8",  
  36.             data: JSON.stringify({order, 'tracking': tracking, 'deposit': deposit, 'id': idOrder }),  
  37.             dataType: "json",  
  38.             success: function () {  
  39.                 alert("Data Added Successfully");  
  40.             },  
  41.             error: function () {  
  42.                 alert("Error while inserting data");  
  43.             }  
  44.         });  
  45.     });  
 

Answers (1)