一个视图访问多个模型,我正在开发中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