In my asp.net MVC application, There is a table that shows some values from the view model.
To update values I thought getting Id and Qty to array and pass to the controller.
Here is my Table
<div class="card-body p-0">
<table id="tblParts" class="table table-striped">
<thead>
<tr>
<th> Part Number </th>
<th> Description </th>
<th> Model </th>
<th> Order Qty </th>
<th></th>
</tr>
</thead>
<tbody> @foreach (var item in Model.OrderBy(i => i.PartNumber)) { <tr>
<td style="display:none;"> @Html.DisplayFor(modelItem => item.Id) </td>
<td> @Html.DisplayFor(modelItem => item.PartNumber) </td>
<td> @Html.DisplayFor(modelItem => item.Description) </td>
<td> @Html.DisplayFor(modelItem => item.Model) </td>
<td id="Qty"> @Html.EditorFor(modelItem => item.OrderQty) </td>
</tr> } </tbody>
</table>
</div>
<div class="card-footer">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="button" value="Update Order" class="btn btn-success" onclick="UpdateOrder();" />
</div>
</div>
</div>
This is the JQuery, Here it gets the Id But it does not get the Qty to the array.
<script >
function UpdateOrder() {
const Orders = $("#tblParts tbody tr").filter(function () {
const cells = $(this).find("td");
return cells.eq(4).text().trim() != ""
}).map(function () {
const cells = $(this).find("td");
return {
[cells.eq(0).text().trim()]: +cells.eq(4).text()
}
}).get()
console.log(Orders);
var newOrders = $.map(Orders, function (element) {
var obj = {};
for (var key in element) {
obj.Id = key;
obj.Quantity = element[key];
}
return obj;
});
}
</script>