gpt4 book ai didi

c# - 在 asp.net core 中通过 ajax 传递数据时获取空列表

转载 作者:行者123 更新时间:2023-12-05 06:53:18 39 4
gpt4 key购买 nike

我正在通过 ajax 将列表数据从 View 发送到 Controller ,数据显示正确,但在 Controller 中收到空对象列表。这是我的代码片段。

这是我的java脚本代码:-

//Collect Multiple List For Pass To Controller
$("#btnSubmit").click(function (e) {
e.preventDefault();
// NavContinue();

var _data = [];
$.each($("#gridTable tbody tr"), function () {
_data.push({
ItemDescription: $(this).find('td:eq(1)').find('input').val(),
ItemCode: $(this).find('td:eq(2)').find('select').val(),
Quantity: $(this).find('td:eq(3)').find('input').val(),

});
});

var data = JSON.stringify({
abc : _data
});
console.log(data);
$.when(Save(data)).then(function (response) {
console.log(response);
}).fail(function (err) {
console.log(err);
});
});

//After Click Save Button Pass All Data View To Controller For Save Database
function Save(abc) {
alert(abc);
return $.ajax({
contentType: 'application/json; charset=utf-8',
//contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
dataType: 'json',
type: 'POST',
url: "/controller/save",
data: abc,
success: function (result) {
if (result) {
alert(result);
}
else {
location.reload();
}
}
});
}

这是我的模型:-

 public class abc
{
public string ItemDescription { get; set; }
public int ItemCode { get; set; }
public int Quantity { get; set; }

}

这是我的 Controller :-

 [HttpPost]
public JsonResult save(abc[] model)
{
var response = model;
return Json(true);
}

如果有人帮助我,我将不胜感激

最佳答案

为了让模型绑定(bind)器处理您在 AJAX 请求中发送的数据,您需要将数组放在您发送的 JSON 字符串的根目录中,而不是在 abc 属性中。试试这个:

var data = JSON.stringify(_data);

关于c# - 在 asp.net core 中通过 ajax 传递数据时获取空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65808727/

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