簡體   English   中英

從View中的腳本調用Controller Action

[英]Call Controller Action from script in View

我試圖從我的腳本調用控制器操作但該方法沒有被調用。

這是我的控制器動作:

 [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);
 } 

這是我的腳本:

<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中調用腳本如下:

<td class=""><img src="" class="img-responsive" onclick="refreshTotal(@product.Id)"></td>

解決方案是在腳本中,從腳本調用沒有進行操作。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM