Denmark Puso

Denmark Puso

  • 1.9k
  • 16
  • 287

Pass the list of data from view to controller not working?

Feb 14 2020 1:07 AM
im trying to pass the list of data from view to controller but when i set break point to controller. only 1 value can pass to controller and that is my problem. see the reference code. thanks 
 
VIEW
  1. <div class="ContainerBanner">  
  2.     <br />  
  3.     <div class="row">  
  4.         <form id="order-frm" action="">  
  5.             @Html.HiddenFor(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 = "enter quantity", @onblur = "displayTextBoxes()", @id = "text-count" })  
  14.                 div>  
  15.             div>  
  16.             <br />  
  17.             <div class="row">  
  18.                 <div class="col-md-12">  
  19.                     <div id="testing-container">div>  
  20.                 div>  
  21.             div>  
  22.   
  23.             <br />  
  24.             <button type="submit" id="btnSubmit" class="btn btn-primary btn-sm"><i class="fas fa-save">i>  Save Emptyboxbutton>  
  25.             <a href="@Url.Action("index", "order")" class="btn btn-default btn-sm"><i class="fa fa-long-arrow-alt-left">i>  Back to lista>  
  26.         form>  
  27.     div>  
  28.   
  29.     <div class="row">  
  30.         <h3 class="orders">Empty box Trackingh3><br />  
  31.         <table id="EmptyBoxList" class="display table table-striped" style="width:100%">  
  32.             <thead>  
  33.                 <tr>  
  34.                     <th style="width: 50%">Clientth>  
  35.                     <th style="width: 25%">Date Deliveredth>  
  36.                     <th style="width: 25%">Tracking Numberth>  
  37.                     <th>Depositth>  
  38.                     <th style="width: 25%">th>  
  39.                 tr>  
  40.             thead>  
  41.             <tbody>tbody>  
  42.         table>  
  43.     div>  
  44. div>  
 Script
 
  1. function displayTextBoxes() {  
  2.         var count = $('#text-count').val();  
  3.   
  4.         var tracking = '<td><input type="text" name="Tracking" class="form-control" id="tracking" placeholder="enter tracking"></td>';  
  5.         var deposit = '<td><input type="text" name="Deposit" class="form-control" id="deposit" placeholder="is with deposit?"></td>';  
  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 model = $('#order-frm').serializeArray()  
  30.             .reduce(function (a, c) {  
  31.                 a[c.name] = c.value;  
  32.                 return a;  
  33.             }, {});  
  34.   
  35.         var idOrder = $('#hid-order-id').val();  
  36.   
  37.         $.ajax({  
  38.             type: 'POST',  
  39.             url: '@Url.Action("addemptybox", "order")',  
  40.             contentType: "application/json; charset=utf-8",  
  41.             data: JSON.stringify({ 'model': model, 'tracking': tracking, 'deposit': deposit, 'id': idOrder }),  
  42.             dataType: "json",  
  43.             success: function () {  
  44.                 alert("Data Added Successfully");  
  45.             },  
  46.             error: function () {  
  47.                 alert("Error while inserting data");  
  48.             }  
  49.         });  
  50.     });  
 Cotroller
 
 
  1. public ActionResult AddEmptyBox(EmptyBoxFormModel model, string[] tracking, string[] deposit, int id)  
  2.         {  
  3.   
  4.             if (ModelState.IsValid)  
  5.             {  
  6.                 try  
  7.                 {  
  8.   
  9.                     var emptyBoxList = new List();  
  10.   
  11.                     for (int i = 0; i < tracking.Length; i++)  
  12.                     {  
  13.                         var box = new EmptyBox  
  14.                         {  
  15.                             ClientId = null,  
  16.                             OrderId = id,  
  17.                             DateDelivered = null,  
  18.                             TrackingNumbers = tracking[i],  
  19.                             Deposit = string.IsNullOrWhiteSpace(deposit[i]) ? 0 : Convert.ToDecimal(deposit[i]),  
  20.                         };  
  21.                         emptyBoxList.Add(box);  
  22.                     }  
  23.                     _emptyBoxService.AddRange(emptyBoxList);  
  24.                     TempData["msg"] = "";  
  25.                     return RedirectToAction("addemptybox");  
  26.                 }  
  27.                 catch (Exception ex)  
  28.                 {  
  29.                     ModelState.AddModelError("CustomError", ex.InnerException.Message);  
  30.                 }  
  31.             }  
  32.   
  33.             model = PrepareEmptyBoxFormModel(model);  
  34.   
  35.             return View(model);  
  36.         }  
 
 

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (3)