2013-02-16 67 views
-2

我想知道爲什麼當我發佈數據到我的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的標籤,模型聯編程序會自動計算出來。

+0

模型綁定到實體是一個強大的反模式,出於安全和體系結構的原因。也許你應該擺脫這一點。 – usr 2013-02-16 22:22:25

+0

公共虛擬字符串標題{get;組; } ...爲什麼字符串是字符串..我的意思是大寫字母? – 2013-02-16 22:24:28

+0

@usr謝謝您的評論,但是,沒有一點闡述,它提供零價值。 – mcottingham 2013-02-16 22:24:43

回答

1

模型聯編程序不知道任何關於ID的信息。它甚至不知道什麼是ID。所以它不能合併從JSON到標籤集合的變化。

我不確定有直接綁定到實體的方式。我建議爲整個結構創建視圖模型(這不是視圖模型類是簡單的DTO)。然後,您需要手動合併可能涉及在視圖模型標記和實體標記之間進行連接的更改。

這是一個很好的系統,即使起初似乎很費力。它給你完全的控制。您甚至可以添加諸如「當且僅當標籤名稱已更改,我將TagNameChangeDateTime設置爲UtcNow」等進程。綁定不能擴展來做這樣的事情。

+1

這就是我最終解決這個問題的方法。謝謝你的幫助! – mcottingham 2013-02-18 21:29:35

相關問題