14

我正在尝试将以下 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 帖子调用的操作的方法签名...

[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; }
}

这是在通用列表中使用的电话对象...

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 数组中填充模型中的列表时,我得到了正确数量的空白对象的列表。

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

4

1 回答 1

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脚本包含到您的页面中。

于 2012-04-11T16:57:22.447 回答