2013-05-04 143 views
0

我有一個BasinPeak實體,其中包含在MVC項目中創建的默認控制器。當我打開http://XX.XX.XX.XX:51573/BasinPeak/Create並添加新BasinPeak時,我該如何在此時爲BasinPeak添加新註釋?MVC實體關係 - 如何在創建實體時添加子實體

我想從NoteController調用創建操作,但創建新Note時,我怎麼能將NoteId傳遞迴BasinPeak?

還是有添加註釋,並將它連接到BasinPeak

public class BasinPeak 
{ 
    public int Id { get; set; } 

    public DateTime DateTime { get; set; } 

    public int Edus { get; set; } 

    public int Rating { get; set; } 

    public int? NoteId { get; set; } 

    public virtual Note Note { get; set; } 
} 

public class Note 
{ 
    public int Id { get; set; } 

    public String Notes { get; set; } 

    public DateTime When { get; set; } 

    public String PersonId { get; set; } 

    public String History { get; set; } 
} 
+0

創建BasicPeak。然後在View for Note創建時,請求用戶希望添加該筆記的basicpeak id。您將在控制器,筆記和基本高峯ID中收到。通過id獲取基本峯值併爲ajax設置註釋 – 2013-05-04 13:20:31

回答

-1

您應創建一個盆地峯先,然後創建連接到該BasinPeak筆記更簡單的方法
如果你希望用戶不要離開創建BasinPeak的頁面,然後嘗試使用理想的應該是這樣的ajax。

you hit url BasinPeak/Create<br> 
form loaded to create basin peak 
you fill in the values and hit submit 
send request using ajax (try using juery form ajax) 
if the creation of the BasinPeak is successfull return true with id of the recently created BasinPeak. 
show him the markup to create a form. because now you have id of the created BasinPeak you can send another request to add the note to the created BasinPeak. 

不過,如果你想創建然後在一次去那麼。你可以嘗試創建一個單獨的視圖模型。包含要創建的BasinPeak的值以及要創建的註釋的值。將強制查看視圖模型設置爲此視圖模型並相應創建峯值和註釋

+0

+1, - 用於解釋的百萬分率。 – 2013-05-04 13:29:05