通过Ajax将数组传递给MVC Action



我的控制器动作有以下代码

[HttpPost]
public async Task<IActionResult> MonthsToAdd(List<string> months)
{
}

我的ajax代码如下

$("#btnSave").on("click", function () {
var months= [];
var value = $("input[name=monthsAvailable]:checked").val()
var lengths = $("input[value=" + value + "]").closest(".row").index()
console.log($("input[value=" + value + "]").closest(".row").index())
for (var i = 0; i <= lengths; i++) {
months.push($(".outer .row:eq(" + i + ") input:radio").val())
}
console.log(JSON.stringify(months));
$.ajax({
contentType: 'application/json;',
dataType: 'json',
type: 'POST',
url: '/AreaName/Controller/MonthsToAdd',
data: JSON.stringify({ 'months': months}),
success: function (response) {
alert("success.");
}
});
});

在浏览器控制台中,我看到了所有正确的参数,但MVC操作没有接收到这些参数。CCD_ 1。我错过了什么?

这就是我端的工作原理:

jQuery ajax方法:

$.ajax({
contentType: "application/json; charset=utf-8",
dataType: 'json',
type: 'POST',
url: '@Url.Action("MonthsToAdd")',
data: JSON.stringify(months),
success: function (response) {
alert("success.");
}
});

控制器动作:

[HttpPost]
public async Task<IActionResult> MonthsToAdd([FromBody] List<string> months)
{
// Add [FromBody]
}

最新更新