Ajax 调用没有将对象传递给控制器
Ajax call doesn't pass object to controller
我正在尝试将字符串传递给 asp.net 核心 3.1 控制器中的 IActionResult 函数。
Ajax:
function CreateApprover() {
const input = {
PlantId: $('#ddlPlant').val(),
UserId: $('#ddlUser').val(),
CategoryId: $('#ddlCategory').val()
};
let jsonData = JSON.stringify(input);
console.log(jsonData);
$.ajax({
contentType: "application/json",
data: jsonData,
traditional: true,
method: "POST",
url: "@Model.CreateUrl",
success: function (data, textStatus, jqXHR) {
dialogDiv.dialog('close');
},
error: function (xhr, err) {
console.log(arguments);
console.log('Request Status:');
console.log(xhr.status);
console.log('Status Text:');
console.log(xhr.status);
console.log(xhr.responseText);
}
});
}
网络信息:
C#:
[HttpPost]
public IActionResult CreateApprover(IFormCollection keyValues, [FromBody]ApproversCreateModel input)
{
return Ok();
}
public class ApproversCreateModel
{
public int UserId { get; set; }
public int PlantId { get; set; }
public int CategoryId { get; set; }
}
payload看起来是正确的,并且在controller方法中打了断点,但是input param一直是null,keyValues参数没有数据。我错过了什么?
Bryan Dellinger 回答,
maybe try PlantId: parseInt($('#ddlPlant').val()), etc.
我正在尝试将字符串传递给 asp.net 核心 3.1 控制器中的 IActionResult 函数。
Ajax:
function CreateApprover() {
const input = {
PlantId: $('#ddlPlant').val(),
UserId: $('#ddlUser').val(),
CategoryId: $('#ddlCategory').val()
};
let jsonData = JSON.stringify(input);
console.log(jsonData);
$.ajax({
contentType: "application/json",
data: jsonData,
traditional: true,
method: "POST",
url: "@Model.CreateUrl",
success: function (data, textStatus, jqXHR) {
dialogDiv.dialog('close');
},
error: function (xhr, err) {
console.log(arguments);
console.log('Request Status:');
console.log(xhr.status);
console.log('Status Text:');
console.log(xhr.status);
console.log(xhr.responseText);
}
});
}
网络信息:
C#:
[HttpPost]
public IActionResult CreateApprover(IFormCollection keyValues, [FromBody]ApproversCreateModel input)
{
return Ok();
}
public class ApproversCreateModel
{
public int UserId { get; set; }
public int PlantId { get; set; }
public int CategoryId { get; set; }
}
payload看起来是正确的,并且在controller方法中打了断点,但是input param一直是null,keyValues参数没有数据。我错过了什么?
Bryan Dellinger 回答,
maybe try PlantId: parseInt($('#ddlPlant').val()), etc.