2012-03-20 66 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()); 
    } 

一旦你的第一個控制器方法的工作,你需要在你的結合模式,爲第二樁法攜手。