2012-04-24 82 views
1

嗨,我想显示一个页面,我允许用户创建一个新的记录并显示下表中相同表格的其他相关记录.....使用mvc3在一个视图中显示创建和详细视图

我需要在Hobbydetail类添加数据: 公共类HobbyDetail {

public virtual HobbyMasters Hobbymaster { get; set; } 
public virtual Course course { get; set; } 
public virtual StudyMedium StudyMedium { get; set; } 
public virtual decimal Fees { get; set; } 

}

我想我的观点“创建”,让用户创建一个新的记录,也显示现有记录在它下面... 我不想使用一个视图模型... 能sumbody帮我 Thanx提前

+0

@glosrob:我跟着这个解决方案http://stackoverflow.com/questions/10136560/how-to-show-two-partials-view-data-on-index-cshtml-mvc3 但它确实帮助我保存我的数据...通过这一切我得到的是一个空对象 – priya77 2012-04-24 12:33:39

回答

0

的一种方式做到这一点是:在你的控制器,创建呈现列表孩子的动作,然后呈现在你的行动使用Html.RenderAction(也是see this)“创建”视图。我在下面包含了一些代码(我没有测试过这个,但它应该给你基本的想法)。请注意,这不是实现此目的的唯一方法 - 您可以使用局部视图see this。还请理解html.RenderAction和html.Action之间的区别,see this

//In HobbyDetail Controller 
    [HTTPGet] 
    public ActionResult Create() 
    { 
     var model = new HobbyDetail(); 
     return View(model); 
    } 

    [HTTPPost] 
    public ActionResult Create(HobbyDetail model) 
    { 
     if(ModelState.isValid) 
     { 
     //logic to persist model 
     } 
     else 
     { 
     //logic when validation fails... 
     } 
    } 

    [ChildActionOnly] 
    public ActionResult ListAll() 
    { 
     List<Hobbydetail> model = //query to DB, or Data store to get Hobbydetails 
     return View(model); 
    } 

//View for ListAll 
@model List<HobbyDetail> 
{ 
Layout = null; //No layout here... 
} 

<ul> 
@foreach(var h in Model) 
{ 
<li>@h.HobbyMasters.Name</li> //for example... 
} 
</ul> 

//View for Create 
@model HobbyDetail 
... 
@{html.renderAction("ListAll");} 
相关问题