gpt4 book ai didi

c# - 通过 json 将 javascript 数组传递给 C#

转载 作者:行者123 更新时间:2023-11-30 20:52:34 25 4
gpt4 key购买 nike

我正在使用这个 javascript 数组:

 var exportOptions = [{
jobName: ""},
{exportType: ""},
{attachToEmail: ""},
{distributorName: ""},
{vistaNumber: ""},
{customerName: ""},
{competitors: ""},
{agreementType: ""},
{ annualPotential: "" },
{businessCase: ""
}];

然后我使用此代码传递给 ASP.NET 代码隐藏 (C#):

                    $.ajax({
type: 'POST',
url: 'Epad.aspx/generateReport',
data: "{'columnList': '" + columnList + "', 'exportOptions':" + JSON.stringify( exportOptions ) + "}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: true,
cache: false,
});

并使用此方法在 C# 中读取:

public static void generateReport(string columnList, Object exportOptions) {}

columnList 是一个字符串变量,这个值我可以从 C# 中检索到,但是我在调​​试器中看不到 exportOptions 的值...我可以在对象 exportOptions(C# Object) 中看到 exportOptions 数组键的名称,但从不传递值...

谁能帮我解决这个问题?

最佳答案

通过这种方式解决了类似的问题:

JS代码(部分处理函数):

    var ords = [];
$(".order-count-input").filter(function() {
return $(this).val() > 0;
}).each(function() {
ords.push({
GoodsId: $(this).attr("goodsId"),
Amount: $(this).val()
});
});

var data = {
orders: ords,
orderId: id
};

var params = {
url: actionUrl,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(data),
success: function (data) {
window.location.replace(data.redirect);
}
};

$.ajax(params);

Controller Action :

[HttpPost]
public JsonResult PostOrder(long orderId, PostOrderViewModel[] orders)

型号:

[Serializable]
public class PostOrderViewModel
{
public long GoodsId { get; set; }

public int Amount { get; set; }
}

关于c# - 通过 json 将 javascript 数组传递给 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com