我想知道爲什麼當我發佈數據到我的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);
}
宋對象如下所示:
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":"[email protected]","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":"[email protected]","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":"[email protected]","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":"[email protected]","ContactPhone":"4035555555","Address":"123 fake street"}}}]}
新正在爲使用現有記錄時的標籤創建記錄。我究竟做錯了什麼?我假設如果我傳遞了包含ID的標籤,模型聯編程序會自動計算出來。
模型綁定到實體是一個強大的反模式,出於安全和體系結構的原因。也許你應該擺脫這一點。 – usr 2013-02-16 22:22:25
公共虛擬字符串標題{get;組; } ...爲什麼字符串是字符串..我的意思是大寫字母? – 2013-02-16 22:24:28
@usr謝謝您的評論,但是,沒有一點闡述,它提供零價值。 – mcottingham 2013-02-16 22:24:43