为什么我从 Ajax 调用传递 JSON 对象作为 ASP.NET Core 中的数据在控制器中获取空对象?
Why I getting null Object in controller from Ajax call passing JSON object as data in ASP.NET Core?
下面的代码是查看页面并调用Ajax方法。我需要传递地址对象。
var address = {};
address.userId = parseInt($("#userID").val());
address.addressLine1 = $("#addAddressLine1").val();
address.addressLine2 = $("#addAddressLine2").val();
address.cityId = parseInt($("#addAddressCity").val());
address.zipCode = $("#addAddressPostCode").val();
address.mobileNo = $("#addAddressPhoneNumber").val();
$.ajax({
type: "POST",
url: "@Url.Action("AddAddress")",
data:JSON.stringify(address),
dataType: 'json',
contentType: 'application/json',
success: function (responce) {
if (responce) {
alert("true");
}
else {
alert("fail");
}
},
failure: function (response) {
alert("failure");
},
error: function (response) {
alert("Something went Wrong");
}
});
这是我的控制器操作方法的代码。当从 Ajax:
调用时,我得到地址对象的 Null
值
public IActionResult AddAddress([FromBody] AddressViewModel address)
{
return Json(address != null);
}
这是我的视图模型:
public class AddressViewModel
{
[JsonPropertyName("userId")]
public int UserId { get; set; }
[JsonPropertyName("addressLine1")]
public string AddressLine1 { get; set; }
[JsonPropertyName("addressLine2")]
public string AddressLine2 { get; set; }
[JsonPropertyName("zipCode")]
public string ZipCode { get; set; }
[JsonPropertyName("mobileNo")]
public string MobileNo { get; set; }
[JsonPropertyName("cityId")]
public int CityId { get; set; }
}
我需要在哪里更改我的代码?我需要更改 StartUp.cs
中的某些内容吗?
需要确保address.userId = parseInt($("#userID").val());
和address.cityId = parseInt($("#addAddressCity").val());
.int类型的值不能接受空值,如果address.userId
或address.cityId
为空,AddressViewModel address
将是 null.You 可以尝试使用 #nullable enable
或更改 address.userId
或 address.cityId
的值。
#nullable enable
public class AddressViewModel
{
[JsonPropertyName("userId")]
public int? UserId { get; set; }
[JsonPropertyName("addressLine1")]
public string AddressLine1 { get; set; }
[JsonPropertyName("addressLine2")]
public string AddressLine2 { get; set; }
[JsonPropertyName("zipCode")]
public string ZipCode { get; set; }
[JsonPropertyName("mobileNo")]
public string MobileNo { get; set; }
[JsonPropertyName("cityId")]
public int? CityId { get; set; }
}
下面的代码是查看页面并调用Ajax方法。我需要传递地址对象。
var address = {};
address.userId = parseInt($("#userID").val());
address.addressLine1 = $("#addAddressLine1").val();
address.addressLine2 = $("#addAddressLine2").val();
address.cityId = parseInt($("#addAddressCity").val());
address.zipCode = $("#addAddressPostCode").val();
address.mobileNo = $("#addAddressPhoneNumber").val();
$.ajax({
type: "POST",
url: "@Url.Action("AddAddress")",
data:JSON.stringify(address),
dataType: 'json',
contentType: 'application/json',
success: function (responce) {
if (responce) {
alert("true");
}
else {
alert("fail");
}
},
failure: function (response) {
alert("failure");
},
error: function (response) {
alert("Something went Wrong");
}
});
这是我的控制器操作方法的代码。当从 Ajax:
调用时,我得到地址对象的Null
值
public IActionResult AddAddress([FromBody] AddressViewModel address)
{
return Json(address != null);
}
这是我的视图模型:
public class AddressViewModel
{
[JsonPropertyName("userId")]
public int UserId { get; set; }
[JsonPropertyName("addressLine1")]
public string AddressLine1 { get; set; }
[JsonPropertyName("addressLine2")]
public string AddressLine2 { get; set; }
[JsonPropertyName("zipCode")]
public string ZipCode { get; set; }
[JsonPropertyName("mobileNo")]
public string MobileNo { get; set; }
[JsonPropertyName("cityId")]
public int CityId { get; set; }
}
我需要在哪里更改我的代码?我需要更改 StartUp.cs
中的某些内容吗?
需要确保address.userId = parseInt($("#userID").val());
和address.cityId = parseInt($("#addAddressCity").val());
.int类型的值不能接受空值,如果address.userId
或address.cityId
为空,AddressViewModel address
将是 null.You 可以尝试使用 #nullable enable
或更改 address.userId
或 address.cityId
的值。
#nullable enable
public class AddressViewModel
{
[JsonPropertyName("userId")]
public int? UserId { get; set; }
[JsonPropertyName("addressLine1")]
public string AddressLine1 { get; set; }
[JsonPropertyName("addressLine2")]
public string AddressLine2 { get; set; }
[JsonPropertyName("zipCode")]
public string ZipCode { get; set; }
[JsonPropertyName("mobileNo")]
public string MobileNo { get; set; }
[JsonPropertyName("cityId")]
public int? CityId { get; set; }
}