2012-04-11 21 views
13

に複雑なJSONモデルを投稿私は、この場合の重要な部分があることで...私のコントローラのアクションメソッドに次のJSONデータを投稿するASP.NET MVC 3 - アクション

{"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}]} 

をしようとしています最後まで "Phones"プロパティ。 FYI:JavaScriptのコンソールでJSON.stringify(profileData)を呼び出してここに投稿したこのJSON文字列を取得しました。 In 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メソッドに到達しており、Phone以外のすべてのプロパティは正しくマッピングされています。 Listオブジェクトには正しい数のPhoneオブジェクトが設定されていますが、すべてnullまたは0またはfalseのすべてのフィールドにデフォルト値が設定されています。だから私は何が間違っているのかよく分かりませんが、JSON配列のモデルでListを設定しようとすると、正しい数の空白のオブジェクトのリストが取得されます。

私がここで間違っていることは何ですか?

答えて

20

適切なコンテンツタイプを設定してみてください、あなたは(JSON.stringifyメソッドを使用して)実際のJSONリクエストを送信していることを確認してください。

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スクリプトを含めることができます。

+4

contentType: 'application/json'が私の問題を修正しました! – Scottie

+0

私の問題は欠けていたcontentTypeでした – Gnomo

+0

これは私を助けました。私はあなたの構文に似て '伝統:真'を落とし、突然働いた。奇妙だが、救済。 –

関連する問題