一個視圖訪問多個模型,我正在開發中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()
請幫我
這真的取決於如何ücontruct UR模式控制器上返回視圖前。幫助你,如果你可以發佈你的控制器。 – tkt986 2012-03-20 04:15:52
@tsegay我已經更新了我的問題,請看看它 – user1274646 2012-03-20 04:38:06
順便說一句,你想你目前正在使用的視圖模型的特定視圖模型,無關的持久性模型。分開他們,一切都會變得更加清晰和簡單 – MikeSW 2012-03-20 08:19:26