守则..
我正在尝试将以下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"}]
这是我的作曲家模特 公共类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; }
}
这是我的联系模式 公共课联系方式: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对象并解析数据并将其映射到我的模型?
有人有任何想法吗?
答案 0 :(得分: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);
}
}
});