Ajax 无法使用带有 2D 数组参数的 C# 控制器操作

Ajax Not Working With C# Controller Action With 2D Array Parameter

本文关键字:参数 数组 操作 控制器 2D Ajax      更新时间:2023-09-26

我的 Ajax 没有命中我的控制器操作。可能是什么原因?

我的控制器操作

 public ActionResult Save_Data(string [][] rosterArray, int Pricelist_Id, int Group_Id)

我的阿贾克斯

$.ajax({
    url: '@Url.Action("Save_Data", "PriceListMaster")',
    type: 'Post',
    async: false,
    contentType: 'application/json',
    dataType: "json",
    data: {  "rosterArray": rosterArray, "Pricelist_Id": "2", "Group_Id": $("#Group_Id").val() },
    //data: JSON.stringify({ "Item_Code": Item_Code, "IPD_Price": IPD_Price, "OPD_Price": OPD_Price, "EMS_Price": EMS_Price }),
    success: function (data) {
        debugger
        if (data.success)
        {
            alert('Data Saved Successfully.');
            location.reload();
        } else {
            alert(data.error_msg);
        }
    }
}

如果在 AJAX 中使用type: 'Post',这意味着您需要在控制器中添加请求类型。

尝试在控制器中使用它,

[HttpPost]
public ActionResult Save_Data(string [][] rosterArray, int Pricelist_Id, int Group_Id)

使用方法中的httpPost定义响应类型。

客户端

$.ajax({
    url: '@Url.Action("Save_Data", "PriceListMaster")',
    type: 'Post',
    async: false,
    contentType: 'application/json',
    dataType: "json",
    data: {  "rosterArray": JSON.stringify(rosterArray), "Pricelist_Id": "2", "Group_Id": $("#Group_Id").val() },
    //data: JSON.stringify({ "Item_Code": Item_Code, "IPD_Price": IPD_Price, "OPD_Price": OPD_Price, "EMS_Price": EMS_Price }),
    success: function (data) {
        debugger
        if (data.success)
        {
            alert('Data Saved Successfully.');
            location.reload();
        } else {
            alert(data.error_msg);
        }
    }
}

您的控制器代码可以是这样的:

[HttpPost]
public ActionResult Save_Data(string rosterArray, int Pricelist_Id, int Group_Id)
{
    string [][] convertedArray = JsonConvert.DeserializeObject<string [][]>(rosterArray);
}

希望这有效注意:但您需要包括using Newtonsoft.Json;