ASP.NET MVC 3 - 将复杂的JSON模型发布到操作中

时间:2012-04-11 16:55:42

标签: asp.net-mvc json asp.net-mvc-3

我正在尝试将以下JSON数据发布到我的控制器上的Action方法...

{
"Id": "0",
"VendorId": "5",
"FirstName": "g",
"LastName": "g",
"Sex": "m",
"DateOfBirth": "",
"Address": "",
"City": "",
"State": "",
"PostCode": "",
"Country": "",
"Email": "",
"AirportCity": "s",
"AirportState": "s",
"CurrencyCode": "1",
"UsTaxId": "",
"ForeignTaxId": "",
"GstNumber": "",
"TaxExemptCalifornia": false,
"TaxExemptCanada": false,
"DateContracted": "",
"DateTerminated": "",
"TerminationStatus": "",
"FirstSeminarDate": "",
"FirstOnsiteDate": "",
"LastSeminarDate": "",
"DateCertified": "",
"CertificationCall": "",
"CertificationMaterials": "",
"FacultyTrainer": "",
"PassportIssued": "",
"PassportExpired": "",
"TnVisa": false,
"BrochurePermission": false,
"Phones": [{
        "Id": 0,
        "Type": "cell",
        "Number": "222-333-4444",
        "IsPrimary": false
    }, {
        "Id": 0,
        "Type": "home",
        "Number": "333-444-5555",
        "IsPrimary": false
    }
]

}

在这种情况下,重要的部分是“电话”属性。 仅供参考:我通过在javascript控制台中调用JSON.stringify(profileData)来获取此JSON字符串。在profileData中是一个实际的对象。

这是发布帖子的ajax电话......

$.ajax({
    url: '/Trainer/SaveProfile',
    type: 'POST',
    dataType: 'json',
    success: function (data) {
        //...
    },
    error: function (jqXHR, textStatus, errorThrown) {
        //...
    },
    data: profileData
});

profileData是我上面发布的JSON。

以下是我使用AJAX帖子调用的Action的方法签名...

[HttpPost]
public ActionResult SaveProfile(TrainerEditModel model)

这是TrainerEditModel(对不起,它太大了)......

public class TrainerEditModel
{
    public TrainerEditModel()
    {
        Phones = new List<Phone>();
    }

    [Display(Name = "Trainer Id")]
    public Int32 Id { get; set; }

    [Display(Name = "Vendor Id")]
    [Required(ErrorMessage = "Vendor ID is required.")]
    public Int32? VendorId { get; set; }

    [Display(Name = "First Name")]
    [Required(ErrorMessage = "First Name is required.")]
    public String FirstName { get; set; }

    [Display(Name = "Last Name")]
    [Required(ErrorMessage = "Last Name is required.")]
    public String LastName { get; set; }

    public String Address { get; set; }

    public String City { get; set; }

    public String State { get; set; }

    [Display(Name = "Postal Code")]
    public String PostCode { get; set; }

    public String Country { get; set; }

    [DataType(DataType.EmailAddress, ErrorMessage = "Must be a valid email address.")]
    public String Email { get; set; }

    [Display(Name = "Airport City")]
    [Required(ErrorMessage = "Airport City is required.")]
    public String AirportCity { get; set; }

    [Display(Name = "Airport State")]
    [Required(ErrorMessage = "Airport State is required.")]
    public String AirportState { get; set; }

    [Display(Name = "Currency Code")]
    [Required(ErrorMessage = "Currency Code is required.")]
    public String CurrencyCode { get; set; }

    [Display(Name = "US Tax Id")]
    [RegularExpression(@"^\d{3}-\d{2}-\d{4}$|^\d{2}-\d{7}$", ErrorMessage = "US Tax Id must be a valid Social Security Number or Tax ID.")]
    public String UsTaxId { get; set; }

    [Display(Name = "Foreign Tax Id")]
    public String ForeignTaxId { get; set; }

    [Display(Name = "Tax Exempt California")]
    public Boolean TaxExemptCalifornia { get; set; }

    [Display(Name = "Tax Exempt Canada")]
    public Boolean TaxExemptCanada { get; set; }

    [Display(Name = "GST Number")]
    public String GstNumber { get; set; }

    [Display(Name = "Gender")]
    [Required(ErrorMessage = "Gender is required.")]
    public String Sex { get; set; }

    [Display(Name = "Date of Birth")]
    [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")]
    public DateTime? DateOfBirth { get; set; }

    [Display(Name = "Date Contracted")]
    [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")]
    public DateTime? DateContracted { get; set; }

    [Display(Name = "Date Terminated")]
    [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")]
    public DateTime? DateTerminated { get; set; }

    [Display(Name = "Termination Status")]
    public String TerminationStatus { get; set; }

    [Display(Name = "First Seminar Date")]
    [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")]
    public DateTime? FirstSeminarDate { get; set; }

    [Display(Name = "First On Site Date")]
    [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")]
    public DateTime? FirstOnsiteDate { get; set; }

    [Display(Name = "Last Seminar Date")]
    [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")]
    public DateTime? LastSeminarDate { get; set; }

    [Display(Name = "Date Certified")]
    [DataType(DataType.Date, ErrorMessage = "Must be a valid date.")]
    public DateTime? DateCertified { get; set; }

    [Display(Name = "Certification Call")]
    public DateTime? CertificationCall { get; set; }

    [Display(Name = "Certification Materials")]
    public DateTime? CertificationMaterials { get; set; }

    [Display(Name = "Faculty Trainer")]
    public String FacultyTrainer { get; set; }

    [Display(Name = "Passport Issued")]
    public DateTime? PassportIssued { get; set; }

    [Display(Name = "Passport Expired")]
    public DateTime? PassportExpired { get; set; }

    [Display(Name = "TN Visa")]
    public Boolean TnVisa { get; set; }

    [Display(Name = "Brochure Permission")]
    public Boolean BrochurePermission { get; set; }

    public List<Phone> Phones { get; set; }
}

这里是通用列表中使用的Phone对象......

public class Phone : IHaveAnId
{
    public Int32 Id { get; set; }
    public String Type { get; set; }
    public String Number { get; set; }
    public Boolean IsPrimary { get; set; }

    public virtual Trainer Trainer { get; set; }
}

所以,这是问题所在。 HTTP Post成功到达Action方法,除了Phones之外的所有属性都正确映射。 List对象甚至填充了正确数量的Phone对象,但它们的所有字段都是null或0或false都有默认值。所以我不确定我做错了什么,但是当我尝试从JSON数组填充模型中的List时,我得到了一个正确数量的空白对象的列表。

我在这里做错了什么想法?

1 个答案:

答案 0 :(得分:21)

尝试设置正确的内容类型,并确保发送真实的JSON请求(使用JSON.stringify方法):

var profileData = {"Id":"0","VendorId":"5","FirstName":"g","LastName":"g","Sex":"m","DateOfBirth":"","Address":"","City":"","State":"","PostCode":"","Country":"","Email":"","AirportCity":"s","AirportState":"s","CurrencyCode":"1","UsTaxId":"","ForeignTaxId":"","GstNumber":"","TaxExemptCalifornia":false,"TaxExemptCanada":false,"DateContracted":"","DateTerminated":"","TerminationStatus":"","FirstSeminarDate":"","FirstOnsiteDate":"","LastSeminarDate":"","DateCertified":"","CertificationCall":"","CertificationMaterials":"","FacultyTrainer":"","PassportIssued":"","PassportExpired":"","TnVisa":false,"BrochurePermission":false,"Phones":[{"Id":0,"Type":"cell","Number":"222-333-4444","IsPrimary":false},{"Id":0,"Type":"home","Number":"333-444-5555","IsPrimary":false}]}

$.ajax({
    url: '/Trainer/SaveProfile',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: JSON.stringify({ model: profileData }),
    success: function (data) {
        //...
    },
    error: function (jqXHR, textStatus, errorThrown) {
        //...
    }
});

JSON.stringify原生内置于现代浏览器中,但如果您需要支持旧版浏览器,则可以在页面中加入json2.js脚本。