1
我想從我的腳本調用控制器動作,但該方法未被調用。從視圖中的腳本調用控制器動作
這是我的控制器操作:
[HttpPost]
public ActionResult EditQuantity(int? id, int quantity)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Cart cart = db.Carts.Find(id);
if (cart == null)
{
return HttpNotFound();
}
cart.Quantity = quantity;
db.SaveChanges();
string url = this.Request.UrlReferrer.AbsolutePath;
return Redirect(url);
}
這是我的腳本:
<td class=""><img src="" class="img-responsive" onclick="refreshTotal(@product.Id)"></td>
解決的辦法是:
<script>
function refreshTotal(ProductId) {
var qty = document.getElementById("product-quantity-" + ProductId).value;
var UnitPrice = document.getElementById("unit-price-" + ProductId).innerText;
var total = qty * UnitPrice;
document.getElementById("product-total-" + ProductId).innerHTML = "$" + total.toFixed(2);
$.post('@Url.Action("EditQuantity", "Home")', { "id": ProductId, "quantity": qty }, function (data) {
});
}
</script>
和腳本是被稱爲從HTML如下在腳本中,從腳本沒有對該動作進行調用。
這不是一個答案。這是一條評論。 – Berkay
對不起,我沒有儘可能多的聲望(50)來回複評論。 –
其實你不需要回答,因爲他在發佈之前解決了他自己的問題。 – Berkay