Javascript 如何在AJAX中使用查询字符串将数据/值传递给控制器

Javascript 如何在AJAX中使用查询字符串将数据/值传递给控制器,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,“pid”值工作正常,控制器上的“数量”值显示为“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

“pid”值工作正常,控制器上的“数量”值显示为“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"
$(“.cartquantity”).change(函数(){
var tblrow=$(this.parents(“.datarow”);
var amtcell=$(“#amtcell”).text();
var pid=$(this.data(“pid”);
变量数量=$(this.val();
var价格=$(“#prc”).text();
$.ajax(
{
url:“/cart/UpdateQuantity?pid=“+pid+”&qty”+qty//此时出现问题
}
).完成(功能(结果){
如果(数量<1)
tblrow.remove();
其他的
amtcell.text(价格*数量);
$(“#cartitems”).text(result.Items)
});
});
控制器代码:
intpid=Convert.ToInt32(Request.QueryString[“pid”]);
整数数量=转换为32(请求查询字符串[“数量])//显示“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)
    });
});

有一个错误,您在url中发送数据时没有使用=before 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"
$(“.cartquantity”).change(函数(){
var tblrow=$(this.parents(“.datarow”);
var amtcell=$(“#amtcell”).text();
var pid=$(this.data(“pid”);
变量数量=$(this.val();
var价格=$(“#prc”).text();
$.ajax(
{
url:“/cart/UpdateQuantity?pid=“+pid+”&qty=“+qty//问题已解决
}
).完成(功能(结果){
如果(数量<1)
tblrow.remove();
其他的
amtcell.text(价格*数量);
$(“#cartitems”).text(result.Items)
});
});
控制器代码:
intpid=Convert.ToInt32(Request.QueryString[“pid”]);
整数数量=转换为32(请求查询字符串[“数量])//显示“0”

查询字符串中的
=
数量缺少
=
,它应该是
“&qty=“+qty
请添加此一次`url:”/cart/UpdateQuantity?pid=“+pid+”&qty=“+qty`谢谢你,欢迎,兄弟:)很高兴帮助你。
int pid = Convert.ToInt32(Request.QueryString["pid"]);
int qty = Convert.ToInt32(Request.QueryString["qty"]); //showing "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 // 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"