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を含むタグを渡すと、モデルバインダーが自動的にそれを把握すると想定しました。