2

编码..

我正在尝试将以下 json 数据发布到我的词曲作者控制器:

id:1
photofilepath:http://songistry.blob.core.windows.net/profileimages/639585169.png
firstname:Mike
lastname:Cottingham
website:http://mikecottingham.ca
minibio:I am the web developer building this website!
publisherid:2
proid:2
contacts:[{"Id":1,"Name":"Mike Cottingham","Phone":"403.919.2706","Email":"mike@mikecottingham.ca"},{"Id":2,"Name":"Bob Thebuilder","Phone":"403.919.2706","Email":"bob@mikecottingham.ca"}]

这是我的歌曲作者模型 public class SongWriter : IEntity { public virtual Int32 Id { get; 放; }

    [Required(ErrorMessage = "First Name is required")]
    [Display(Name = "First Name")]
    [StringLength(64, ErrorMessage = "First Name cannot exceed 64 characters.")]
    public virtual String FirstName { get; set; }

    [Required(ErrorMessage = "Last Name is required")]
    [Display(Name = "Last Name")]
    [StringLength(64, ErrorMessage = "Last Name cannot exceed 64 characters.")]
    public virtual String LastName { get; set; }

    [Display(Name = "Website")]
    [StringLength(128, ErrorMessage = "Website cannot exceed 128 characters.")]
    public virtual String Website { get; set; }

    [Display(Name = "Mini Bio")]
    [StringLength(128, ErrorMessage = "Mini Bio cannot exceed 128 characters.")]
    public virtual String MiniBio { get; set; }

    [Display(Name = "Bio")]
    [StringLength(4096, ErrorMessage = "Bio cannot exceed 4096 characters.")]
    public virtual String Bio { get; set; }

    [Display(Name = "Photo")]
    [StringLength(256, ErrorMessage = "File Path cannot exceed 256 characters.")]
    public virtual String PhotoFilePath { get; set; }

    [Column("DefaultPublisherId")]
    public virtual Int32? PublisherId { get; set; }

    [Column("DefaultProId")]
    public virtual Int32? ProId { get; set; }

    [Display(Name = "Invitation Email")]
    [StringLength(128, ErrorMessage = "Invitation Email cannot exceed 128 characters.")]
    public virtual String InvitationEmail { get; set; }

    [StringLength(128, ErrorMessage = "Invitation Code cannot exceed 128 characters.")]
    public virtual String InvitationSecretKey { get; set; }

    public virtual Int32? LoginId { get; set; }

    public virtual Login Login { get; set; }
    public virtual ICollection<SongWriterSong> SongWriterSongs { get; set; }

    public virtual Pro Pro { get; set; }
    public virtual Publisher Publisher { get; set; }

    public virtual ICollection<Contact> Contacts { get; set; }
}

这是我的联系人模型 public class Contact : IEntity { public virtual Int32 Id { get; 放; }

    [DisplayName("Name")]
    [Required(ErrorMessage = "Contact name is required.")]
    [StringLength(128, ErrorMessage="Contact name can not exceed 128 characters")]
    public virtual String Name { get; set; }

    [DisplayName("Phone Number")]
    [Required(ErrorMessage = "Contact phone number is required.")]
    [StringLength(32, ErrorMessage = "Contact phone number can not exceed 32 characters")]
    public virtual String Phone { get; set; }

    [DisplayName("Email Address")]
    [Required(ErrorMessage = "Contact email address is required.")]
    [StringLength(128, ErrorMessage = "Contact email address can not exceed 128 characters")]
    public virtual String Email { get; set; }

    public virtual SongWriter SongWriter { get; set; }
}

我使用以下代码将数据发布到控制器

self.savechanges = function () { var model = { id: self.songwriter.id(), photofilepath: self.songwriter.photofilepath(), firstname: self.songwriter.firstname(), lastname: self.songwriter.lastname( ), 网站: self.songwriter.website(), minibio: self.songwriter.minibio(), bio: self.songwriter.bio(), publisherid: self.songwriter.publisherid(), proid: self.songwriter.proid( ) };

return $.post("/songwriter/edit", model, function (response) {
    self.isvalid(response.Success);
    if (!self.isvalid()) {
        helpers.errordialog(response.Errors);
    }
    else {
        self.songwriter.id(response.Data.Id);
    }
}, "json");

};

我收到验证错误,说需要联系人姓名,需要联系人电子邮件......等等。 MVC 引擎不应该查看 JSON 对象并解析数据并将其映射到我的模型吗?

有人有什么想法吗?

4

1 回答 1

1

弄清楚了!

我需要使用 JQuery.ajax,而不是 JQuery.post 函数。JQuery.post 函数未正确标记请求的 contentType 和 dataType。

工作代码:

return $.ajax("/songwriter/edit", {
                data: JSON.stringify(model),
                type: "POST",
                contentType: "application/json",
                dataType: "json",
                success: function (response) {
                    self.isvalid(response.Success);
                    if (!self.isvalid()) {
                        helpers.errordialog(response.Errors);
                    }
                    else {
                        self.songwriter.id(response.Data.Id);
                    }
                }
            });
于 2013-01-30T03:09:19.143 回答