2012-03-20 62 views
0

一个视图访问多个模型,我正在开发中MVC3的应用程序。 基本上它是一个问题论坛,在那里提问页面上,用户将看到在end.and所有问题的环节和岗位评论框,当他点击链接的问题,他移动到答案page.now答案页面使用另一种模式类和他们我无法获得我的答案类数据怎样才能MVC3

我发现每个控制器有一个视图和一个模型 但我希望我的视图访问多个模型,我不知道如何去了解它..

我尝试这样做:

public class MainClass 
{ 
    public Question_Page question{ get; set; } 
    public Answer_Page answer { get; set; } 
} 
    public class Question_Page 
{ 
    public virtual int Id { get; set; } 
    public virtual string Question_name { get; set; } 
    public virtual DateTime Created_Date { get; set; } 
    public virtual DateTime Modified_Date { get; set; } 
    public virtual int Created_By { get; set; } 
    public virtual int Modified_By { get; set; } 
    public virtual char Deleted { get; set; } 
    public Question_Page() 
    { 
     this.Deleted='F'; 
    } 
} 

public class Answer_Page 
    { 
    public virtual int Id { get; set; } 
    public virtual string Answer { get; set; } 
    public virtual DateTime Created_Date { get; set; } 
    public virtual DateTime Modified_Date { get; set; } 
    public virtual int Created_By { get; set; } 
    public virtual int Modified_By { get; set; } 
    public virtual char Deleted { get; set; } 
    public virtual Question_Page Question { get; set; } 
    public Answer_Page() 
    { 
     this.Deleted='F'; 
    } 
} 

现在的视图中问题清单在哪里显示的问题清单:做这一点我很刚开errorin行后

@model IEnumerable<Core.Model.MainPage> 
@{ 
    ViewBag.Title = "Index"; 
} 
<style type="text/css"> 
ul 
{ 
    list-style-type: none; 
} 
</style> 
<h2>All Questions</h2> 
<hr /> 

@using (Html.BeginForm("Index","Question",FormMethod.Post)) 
{ 
    @Html.ValidationSummary(true) 
    <ul> 
    @foreach (var item in Model) 
    { 

    <li>@Html.ActionLink(item.Question_name, "answer", new { Qry = item.Id })</li> 
    } 
    </ul> 

    <label for="PostyourQuestion:">Post your Question:</label><br /><br /> 
    @Html.TextArea("textID")  
    <br /> 
    <input type="submit"/> 
    } 

: @foreach(以型号VAR项目)

这是我的控制器:

public ActionResult Index() 
    { 
     return View(new StudentService().GetAllStudents()); 
    } 
    [HttpPost] 
    public ActionResult Index(Question_Page question,string textID) 
    {   
     question.Question_name = textID; 
     question.Created_Date = DateTime.Now; 
     question.Modified_Date = DateTime.Now; 
     question.Created_By = 101; 
     question.Modified_By = 101; 
     new StudentService().SaveOrUpdateStudent(question); 
     return View(new StudentService().GetAllStudents()); 
    } 

StudentService是我HV定义的方法GetAllStudents()类,SaveOrUpdateStudent()

请帮我

+0

这真的取决于如何ücontruct UR模式控制器上返回视图前。帮助你,如果你可以发布你的控制器。 – tkt986 2012-03-20 04:15:52

+0

@tsegay我已经更新了我的问题,请看看它 – user1274646 2012-03-20 04:38:06

+0

顺便说一句,你想你目前正在使用的视图模型的特定视图模型,无关的持久性模型。分开他们,一切都会变得更加清晰和简单 – MikeSW 2012-03-20 08:19:26

回答

0

你的回答很简单,我想呢?

@foreach (var item in Model) 

应该

@foreach (var item in Model.Question_Page) 

你的模型是MainPage

您可以发布该模型在这里呢?

+0

我试过了,但仍然发生错误: MainPage不包含Question_Page的定义 – user1274646 2012-03-20 04:36:36

0

假设GetAllStudents()在你的控制器返回IEnumerable<Student>

return View(new StudentService().GetAllStudents()); 

实际模式发送到View是IEnumerable<Student>

,如果你需要IEnumerable<Core.Model.MainPage>为模型只是从你的控制器

返回合适的对象
IEnumerable<Core.Model.MainPage> model = //... define function that return Enumerable MainPage 
return View(model); 
0

如果你想用同一型号的不同视图

public ActionResult Index() 
    { 
     //I am assuming, You are returning IEnumerable<MainClass> 
     List<MainClass> mainClass=new StudentService().GetAllStudents(); 
     if(mainClass==null) 
      mainClass=new List<MainClass>(); //Incase you are not checking null value return 
     return View(mainClass); //return a none null IEnumerable<MainClass> object 
    } 

    [HttpPost] 
    public ActionResult Index(Question_Page question,string textID) 
    {   
     question.Question_name = textID; 
     question.Created_Date = DateTime.Now; 
     question.Modified_Date = DateTime.Now; 
     question.Created_By = 101; 
     question.Modified_By = 101; 
     new StudentService().SaveOrUpdateStudent(question); 
     return View(new StudentService().GetAllStudents()); 
    } 

一旦你的第一个控制器方法的工作,你需要在你的结合模式,为第二桩法携手。