jquery - JSON 数组未绑定(bind)到 MVC 模型

标签 jquery arrays json ajax asp.net-mvc

我通过 AJAX 调用将 json 数据列表传递给 Controller ​​。 字符串数组“LandPhone、Mobile 和 Email”在 Controller 中变为空。其实还是有一些值(value)的。

主要型号

public class CustomerModel
{
    public string ReferenceId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string TINNo { get; set; }
    public string CurrencyId { get; set; }
    public List<CustomerAddressModel> Address { get; set; }

}

子模型

public class CustomerAddressModel
{
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public int ZipCode { get; set; }
    public string District { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string[] LandPhone { get; set; }
    public string[] Mobile { get; set; }
    public string[] Email { get; set; }

}

AJAX 调用

function get() {
$.ajax({
    type: 'POST',
    url: '/temp/Customer',
    data: { "ReferenceId": "", "FirstName": "", "MiddleName": "", "LastName": "", "TINNo": "", "CurrencyId": 0,
            "Address": [{
                "AddressLine1": "", "AddressLine2": "", "AddressLine3": "", "ZipCode": 0, "District": "", "State": "", "Country": "",
                "LandPhone": ["123"],
                "Mobile": ["1234567890", "9876543210"],
                "Email": ["a@b.com", "b@c.com"]
            }]
     },
    dataType: "json",
    //contentType: "application/json;charset=utf-8",
    async: false,
    success: function (data) {
        alert(data);
    }
});

在 Controller 方法数据中变得像这样

In the Controller Method Data getting like this

最佳答案

您需要包含 contentType: "application/json;charset=utf-8", 选项并对数据进行字符串化

$.ajax({
    type: 'POST',
    url: '@Url.Action("Customer", "temp")', // don't hard code
    contentType: "application/json;charset=utf-8"
    data: JSON.stringify({ "ReferenceId": ..... "Address": [{ "AddressLine1": "" ... }]}),
    ....

关于jquery - JSON 数组未绑定(bind)到 MVC 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098209/

相关文章:

javascript - 这个数据的格式是什么?它是自定义格式吗?

arrays - 使用 6 种面额找零的快速算法 : Interview Practice

python - DataFrame 到 Json 使用第一列作为键,第二列作为值

javascript - 无法通过 jQuery 正确定位两个不同的下拉菜单

javascript - 如何使用 jquery 从 <td> 中获取隐藏值

javascript - jQuery 不会在调整大小时更改高度属性

c - 用C读取文件并将数据存储在数组中

c - 无法将文件读入 C 中的结构数组

javascript - 动态脚本标签安全

javascript - 如何使用 AJAX 显示内部 JSON 数据数组的数据?