简体   繁体   English

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

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

My Ajax is not hitting my controller action. 我的Ajax没有达到我的控制器动作。 What could be the reason? 可能是什么原因?

My Controller Action 我的控制器动作

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

My Ajax 我的阿贾克斯

$.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);
        }
    }
}

If you are using type: 'Post' in AJAX, that means you need to add a Request type in Controller. 如果您使用type: 'Post'是AJAX中的type: 'Post' ,则意味着您需要在Controller中添加请求类型。

Try using this in controller, 尝试在控制器中使用它,

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

Use httpPost in the Method for define type of response. 在方法中使用httpPost定义响应类型。

at client side 在客户端

$.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);
        }
    }
}

and your controller code can be like this: 您的控制器代码可以像这样:

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

Hope this works Note: But you need to incude using Newtonsoft.Json; 希望这行得通注意:但是您需要using Newtonsoft.Json;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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