我想弄清楚为什么当我将数据发布到我的MVC4控制器时,应该在使用现有记录时创建新记录。
我的控制器如下:
public ActionResult Edit(Song song) {
JsonBase jsonBase;
var dbSong = _songRepository.FindById(song.Id);
TryUpdateModel(dbSong, String.Empty, null, new String[] { "CreatedDate", "SongAlternateTitles", "SongWriterSongs", "Exploitations" });
if (ModelState.IsValid) {
_unitOfWork.Commit();
jsonBase = new JsonBase(true, null, new { Id = dbSong.Id });
}
else {
jsonBase = new JsonBase(false, ModelState.Values);
}
return Json(jsonBase);
}
Song对象如下所示:
public class Song : IEntity
{
public virtual Int32 Id { get; set; }
[Required]
[StringLength(128, ErrorMessage = "Song Title cannot exceed 128 characters")]
public virtual String Title { get; set; }
[Required]
[StringLength(512, ErrorMessage = "Song Description cannot exceed 512 characters")]
public virtual String Description { get; set; }
[StringLength(8000, ErrorMessage="Song lyrics cannot exceed 4096 characters")]
public virtual String Lyrics { get; set; }
[Required]
[Display(Name="Song Status")]
public virtual Boolean isMaster { get; set; }
[StringLength(256, ErrorMessage = "File Path cannot exceed 256 characters.")]
public virtual String AudioFilePath { get; set; }
[StringLength(256, ErrorMessage = "File Path cannot exceed 256 characters.")]
public virtual String CoverImageFilePath { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual Int32? BPM { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<SongAlternateTitles> SongAlternateTitles { get; set; }
public virtual ICollection<Exploitation> Exploitations { get; set; }
public virtual ICollection<SongWriterSong> SongWriterSongs { get; set; }
public virtual ICollection<Tweak> Tweaks { get; set; }
}
我传入的JSON数据如下所示:
{"id":681,"title":"Another You","description":"Cascada - Everytime We Touch","lyrics":"","ismaster":false,"audiofilepath":"http://songistry.blob.core.windows.net/audiosamples/3211556309.mp3","coverimagefilepath":"http://songistry.blob.core.windows.net/coverimages/2711679216.jpg","tags":[{"id":43,"name":"Edgy","description":"This music is on the edge!","valid":true,"editing":false,"checked":true},{"id":44,"name":"Pop","description":"This music pops!","valid":true,"editing":false,"checked":true},{"id":47,"name":"Positive","description":"Positive","valid":true,"editing":false,"checked":true},{"id":45,"name":"Quirky","description":"Quirky","valid":true,"editing":false,"checked":true},{"id":46,"name":"R&B","description":"R&B","valid":true,"editing":false,"checked":true}],"songalternatetitles":[],"exploitations":[],"songwritersongs":[{"Id":742,"Pro":{"Id":2,"Name":"ASCAP"},"Publisher":{"Id":2,"Name":"Some Publisher","Phone":"4035555555","ContactName":"Some Guy","ContactEmail":"guy@songistry.com","ContactPhone":"4035555555","Address":"123 fake street"},"Percentage":100,"SongWriter":{"Id":1,"FirstName":"Mike","LastName":"Cottingham","Name":"Mike Cottingham","Pro":{"Id":2,"Name":"ASCAP"},"Publisher":{"Id":2,"Name":"Some Publisher","Phone":"4035555555","ContactName":"Some Guy","ContactEmail":"guy@songistry.com","ContactPhone":"4035555555","Address":"123 fake street"}}},{"Id":771,"Pro":{"Id":206,"Name":"ACUM"},"Publisher":{"Id":2,"Name":"Some Publisher","Phone":"4035555555","ContactName":"Some Guy","ContactEmail":"guy@songistry.com","ContactPhone":"4035555555","Address":"123 fake street"},"Percentage":0,"SongWriter":{"Id":8,"FirstName":"Jessica","LastName":"Cottingham","Name":"Jessica Cottingham","Pro":{"Id":1,"Name":"Socan"},"Publisher":{"Id":2,"Name":"Some Publisher","Phone":"4035555555","ContactName":"Some Guy","ContactEmail":"guy@songistry.com","ContactPhone":"4035555555","Address":"123 fake street"}}}]}
当应使用现有记录时,正在为标签创建新记录。我究竟做错了什么?我假设如果我传递包含ID的标签,那么模型绑定器会自动计算出来。
答案 0 :(得分:1)
模型绑定器对ID没有任何了解。它甚至不知道ID是什么。因此,它无法合并从JSON到Tags集合的更改。
我不确定是否存在直接绑定到实体的方法。我建议为整个结构创建视图模型(由于视图模型类是微不足道的DTO,因此没有那么多工作)。然后,您需要手动合并可能涉及在视图模型标记和实体标记之间进行连接的更改。
这是一个很好的系统,即使它最初看起来很费劲。它给你完全控制。你甚至可以添加诸如&#34之类的进程;当且仅当标签名称改变时,我将TagNameChangeDateTime设置为UtcNow&#34;。绑定不能扩展到做这些事情。