Hello there! I am new to asp.net mvc and I'm looking for a way to pass JSonResult data to a controller method. In my View, I have set up a table like this:
<table id="tbl_orderItems" class="table table-striped table-bordered table-hover dt-responsive nowrap" cellspacing="0">
<thead>
<tr>
<th>Item Code</th>
<th>Quantity</th>
<th>Unit</th>
<th>Item Description</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
@if (TempData["listOrder"] != null)
{
foreach (var items in TempData["listOrder"] as List<ebms.ViewModels.OrderItems>)
{ @Html.HiddenFor(modelItem => items.Id, new { @id = "productId" })
<tr>
<td id="productCode">@items.PRODUCT_CODE</td>
<td contenteditable="true" id="quantity">@items.QUANTITY_ORDERED
<input id="save" type="button" value="Save" />
</td>
<td id="unit">@items.MEASUREMENT_NAME</td>
<td id="itemDesc">@items.ITEM_DESCRIPTION</td>
<td id="status">@items.tmpStatus</td>
<td>
@Html.ActionLink("Remove", "Remove", new { id = items.Id },
new { onclick = "return confirm('Are sure want to remove?');" })
</td>
</tr>
}
}
</table>
The table values are populated from another source. However, the Quantity column has a default value of 0 as it needs to be inputted manually in the table.
Therefore, I have made the QUANTITY_ORDERED column editable so that users can enter the quantity directly in the table:
<script>
$(document).ready(function () {
$("#save").prop("disabled", true);
$("div").on('input', function () {
$("#save").prop("disabled", false);
});
$("#save").click(function () {
var modifiedQuanity = {};
modifiedQuanity.Id = $("#productId").text();
modifiedQuanity.QUANTITY_ORDERED = $("#quantity").text();
$.post("/Sales/Save", modifiedQuanity, function (msg) {
$("#save").prop("disabled", true);
$("#msg").html("<strong>" + msg + "</strong>");
});
});
});
</script>
In my Controller, I have a JSonResult Method named Save which is intended to receive the QUANTITY_ORDERED value from the view.
public JsonResult Save(ORDER_DETAILS obj)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
ORDER_DETAILS existing = db.ORDER_DETAILS.Find(obj.PRODUCT_ID);
existing.QUANTITY_ORDERED = obj.QUANTITY_ORDERED;
db.SaveChanges();
return Json("Order saved successfully!");
}
}
I need to then pass the JsonResult data to a Controller Method for final saving.
However, I am facing an issue where the value doesn't seem to get passed to the Save Method. Can you help me figure out what I might be missing in order to successfully pass the JsonResult data to the Controller Method?