簡體   English   中英

如何使用 AJAX 中的查詢字符串將數據/值傳遞給 controller

[英]How to pass data/value to controller by using query string in AJAX

“pid”值工作正常,“qty”值在 controller 顯示為“0”。

我 100% 確定問題出在查詢字符串上。

以下是 ajax 和 jquery 代碼:

$(".cartquantity").change(function () {
    var tblrow = $(this).parents(".datarow");
    var amtcell = $("#amtcell").text();
    var pid = $(this).data("pid");
    var qty = $(this).val();
    var price = $("#prc").text();
    $.ajax(
        {
            url: "/cart/UpdateQuantity?pid=" + pid + "&qty" + qty //here problem occurs
        }
    ).done(function (result) {
        if (qty < 1)
            tblrow.remove();
        else
            amtcell.text(price * qty);
        $("#cartitems").text(result.Items)
    });
});

Controller Code:

int pid = Convert.ToInt32(Request.QueryString["pid"]);
int qty = Convert.ToInt32(Request.QueryString["qty"]); //showing "0"

請告訴我通過查詢字符串傳遞值的正確語法是什么。 因為 qty 變量顯示正確的值,但是當數據通過查詢字符串時,它變為 0。

請試試這個

$(".cartquantity").change(function () {
    var tblrow = $(this).parents(".datarow");
    var amtcell = $("#amtcell").text();
    var pid = $(this).data("pid");
    var qty = $(this).val();
    var price = $("#prc").text();
    $.ajax(
        {
            url: "/cart/UpdateQuantity?pid=" + pid + "&qty=" + qty //here problem occurs
        }
    ).done(function (result) {
        if (qty < 1)
            tblrow.remove();
        else
            amtcell.text(price * qty);
        $("#cartitems").text(result.Items)
    });
});
 Controller Code:
int pid = Convert.ToInt32(Request.QueryString["pid"]);
int qty = Convert.ToInt32(Request.QueryString["qty"]); //showing "0"

有一個錯誤,在 url 中發送數據時,您沒有在 qty 之前使用 =

$(".cartquantity").change(function () {
    var tblrow = $(this).parents(".datarow");
    var amtcell = $("#amtcell").text();
    var pid = $(this).data("pid");
    var qty = $(this).val();
    var price = $("#prc").text();
    $.ajax(
        {
            url: "/cart/UpdateQuantity?pid=" + pid + "&qty=" + qty // problem Solved
        }
    ).done(function (result) {
        if (qty < 1)
            tblrow.remove();
        else
            amtcell.text(price * qty);
        $("#cartitems").text(result.Items)
    });
});

Controller Code:

int pid = Convert.ToInt32(Request.QueryString["pid"]);
int qty = Convert.ToInt32(Request.QueryString["qty"]); //showing "0"

暫無
暫無

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

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