2016-09-09 41 views
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如下在腳本中,從腳本沒有對該動作進行調用。

回答

0

我在'productId'中有一個輸入錯誤 - 應該是'ProductId'。它現在正在工作:)

0

在您提供的代碼中一切似乎都很好。您是否在瀏覽器控制檯中查找了任何javascript錯誤,並且您的操作在右側控制器中?

+0

這不是一個答案。這是一條評論。 – Berkay

+0

對不起,我沒有儘可能多的聲望(50)來回複評論。 –

+0

其實你不需要回答,因爲他在發佈之前解決了他自己的問題。 – Berkay

相關問題