Below is the controller method:
[HttpPost]
public ActionResult CheckInventory(int itemID, short quantity)
{
Item model = db.Items.Single(x => x.Item_ID == itemID);
model.Quantity_In_Stock = quantity;
db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
db.SaveChanges();
db.Refresh(System.Data.Objects.RefreshMode.ClientWins, model);
var newModel = db.Items.Single(x => x.Item_ID == itemID);
return View("Details", newModel);
}
Here is the AJAX code snippet:
<script src="/Scripts/jquery-1.5.1.min.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$(".UpdateItem").click(function () {
var stockQuantity = 123;
var iId = $(this).attr("data-id");
if (iId != '' || iId != null) {
$.post("/Item/CheckInventory", { "itemID": iId, "quantityInStock": stockQuantity },
function (data) {
$('#quantity-in-stock').text(stockQuantity);
});
}
else {
alert("Item ID is empty");
}
});
});
</script>
The link to trigger the action:
<div>
<a href="#" class="UpdateItem" data-id="@Model.Item_ID">Update</a>
</div>
Displayed table with updated information:
<fieldset>
<legend>Item Details</legend>
<div class="display-label">
<table>
<tr><td>Quantity in Stock:</td><td><div id="quantity-in-stock">@Html.DisplayFor(model => model.Quantity_In_Stock)</div></td></tr>
</table>
</div>
</fieldset>
Despite no error messages, clicking the link does not trigger any action and only adds a # to the URL. Both breakpoints in the controller's CheckInventory method and the AJAX function are not being reached. Any assistance would be greatly appreciated.