我试图将以下
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}]}
在这种情况下,重要的部分是“手机”财产.
FYI:我通过在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.
[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数组中填充一个模型列表时,我得到一个正确数量的空白对象的列表.
任何想法我在这里做错什么?
解决方法
尝试设置正确的内容类型,并确保您正在发送一个真正的JSON请求(使用JSON.stringify方法):
var profileData = {"Id":"0","IsPrimary":false}]} $.ajax({ url: '/Trainer/SaveProfile',contentType: 'application/json',data: JSON.stringify({ model: profileData }),errorThrown) { //... } });
JSON.stringify本来是内置于现代浏览器中的,但如果您需要支持旧版浏览器,则可以将json2.js脚本包含在您的页面中.