Reputation: 689
Here is my controller method.
[HttpPost]
public ActionResult CheckStock(int productId, short units)
{
Product model = db.Products.Single(x => x.Product_ID == productId);
model.Units_In_Stock = units;
db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
db.SaveChanges();
db.Refresh(System.Data.Objects.RefreshMode.ClientWins, model);
var newModel = db.Products.Single(x => x.Product_ID == productId);
return View("Details", newModel);
}
Here is my AJAX:
<script src="/Scripts/jquery-1.5.1.min.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$(".UpdateProductId").click(function () {
var unitsInStock = 123;
var pId = $(this).attr("data-id");
if (pId != '' || pId != null) {
$.post("/Product/CheckStock", { "productId": pId, "unitsInStock": unitsInStock },
function (data) {
$('#units-in-stock').text(unitsInStock);
});
}
else {
alert("Product_ID is empty");
}
});
});
</script>
Here is my Link:
<div>
<a href="#" class="UpdateProductId" data-id="@Model.Product_ID">Update</a>
</div>
Here is the trimmed down table with the only affected row.
<fieldset>
<legend>Product</legend>
<div class="display-label">
<table>
<tr><td>Units_In_Stock</td><td><div id="units-in-stock">@Html.DisplayFor(model => model.Units_In_Stock)</div></td></tr>
</table>
</div>
</fieldset>
I'm getting no error at all, but nothing at all is happening when I click the link except a # is being added to the querystring. The breakpoint in CheckStock in the controller and the breakpoint in the AJAX method are not being hit. Please help.
Upvotes: 2
Views: 797
Reputation: 97672
Your key names and your CheckStockmethod parameters are not the same, you have unitsInStock
in your ajax request but units
in your CheckStock method, change them to match.
Upvotes: 2