2013-05-04 89 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