简体   繁体   中英

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

The "pid" value is working fine by "qty" value is showing as "0" at controller.

I'm 100% sure that the problem is with the query string.

Here are the ajax and jquery code:

$(".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"

Please tell me how whats the correct syntax of passing values through query string. Because qty variable is showing correct value but when the data pass through query string it becomes 0.

Please try this

$(".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"

There is one mistake,You are not using = before qty while send data in url

$(".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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM