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
- <div class="ContainerBanner">
- <br />
- <div class="row">
- <form id="order-frm" action="">
- @Html.HiddenFor(x => x.Id, new { @id = "hid-order-id" })
- @Html.Raw(TempData["msg"])
-
- @Html.Partial("_Message")
-
- <div class="row">
- <div class="col-md-2">
- @Html.LabelFor(x => x.Quantity, new { @class = "form-label" })
- @Html.TextBoxFor(x => x.Quantity, new { @class = "form-control", @placeholder = "enter quantity", @onblur = "displayTextBoxes()", @id = "text-count" })
- div>
- div>
- <br />
- <div class="row">
- <div class="col-md-12">
- <div id="testing-container">div>
- div>
- div>
-
- <br />
- <button type="submit" id="btnSubmit" class="btn btn-primary btn-sm"><i class="fas fa-save">i> Save Emptyboxbutton>
- <a href="@Url.Action("index", "order")" class="btn btn-default btn-sm"><i class="fa fa-long-arrow-alt-left">i> Back to lista>
- form>
- div>
-
- <div class="row">
- <h3 class="orders">Empty box Trackingh3><br />
- <table id="EmptyBoxList" class="display table table-striped" style="width:100%">
- <thead>
- <tr>
- <th style="width: 50%">Clientth>
- <th style="width: 25%">Date Deliveredth>
- <th style="width: 25%">Tracking Numberth>
- <th>Depositth>
- <th style="width: 25%">th>
- tr>
- thead>
- <tbody>tbody>
- table>
- div>
- div>
Script
- function displayTextBoxes() {
- var count = $('#text-count').val();
-
- var tracking = '<td><input type="text" name="Tracking" class="form-control" id="tracking" placeholder="enter tracking"></td>';
- var deposit = '<td><input type="text" name="Deposit" class="form-control" id="deposit" placeholder="is with deposit?"></td>';
-
- for (i = 1; i <= count; i++)
- {
- $('#testing-container').append('' + tracking + deposit + '');
- }
- }
-
- var tracking = [];
- var deposit = [];
-
- function getAllData() {
- $('#testing-container').each(function () {
- var t = $(this).find('#tracking').val();
- var d = $(this).find('#deposit').val();
-
- tracking.push(t);
- deposit.push(d);
- });
- }
-
- $('#btnSubmit').click(function () {
-
- getAllData();
- var model = $('#order-frm').serializeArray()
- .reduce(function (a, c) {
- a[c.name] = c.value;
- return a;
- }, {});
-
- var idOrder = $('#hid-order-id').val();
-
- $.ajax({
- type: 'POST',
- url: '@Url.Action("addemptybox", "order")',
- contentType: "application/json; charset=utf-8",
- data: JSON.stringify({ 'model': model, 'tracking': tracking, 'deposit': deposit, 'id': idOrder }),
- dataType: "json",
- success: function () {
- alert("Data Added Successfully");
- },
- error: function () {
- alert("Error while inserting data");
- }
- });
- });
Cotroller
- public ActionResult AddEmptyBox(EmptyBoxFormModel model, string[] tracking, string[] deposit, int id)
- {
-
- if (ModelState.IsValid)
- {
- try
- {
-
- var emptyBoxList = new List();
-
- for (int i = 0; i < tracking.Length; i++)
- {
- var box = new EmptyBox
- {
- ClientId = null,
- OrderId = id,
- DateDelivered = null,
- TrackingNumbers = tracking[i],
- Deposit = string.IsNullOrWhiteSpace(deposit[i]) ? 0 : Convert.ToDecimal(deposit[i]),
- };
- emptyBoxList.Add(box);
- }
- _emptyBoxService.AddRange(emptyBoxList);
- TempData["msg"] = "";
- return RedirectToAction("addemptybox");
- }
- catch (Exception ex)
- {
- ModelState.AddModelError("CustomError", ex.InnerException.Message);
- }
- }
-
- model = PrepareEmptyBoxFormModel(model);
-
- return View(model);
- }