Asp.net mvc 一个视图如何访问mvc3中的多个模型

Asp.net mvc 一个视图如何访问mvc3中的多个模型,asp.net-mvc,asp.net-mvc-3,razor,Asp.net Mvc,Asp.net Mvc 3,Razor,我正在用MVC3开发一个应用程序。 基本上,这是一个问题论坛,在问题页面上,用户将看到所有问题作为链接,并在最后显示一个帖子评论框。当他单击问题链接时,他将移动到答案页面。现在答案页面使用另一个模型类,他们的“我无法访问我的答案类数据” 我发现每个控制器都有一个视图和一个模型 但是我希望我的观点能够访问多个模型,我不知道该怎么做 我试过这个: public class MainClass { public Question_Page question{ get; set; }

我正在用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';
    }
}
现在,在问题列表视图中,我将显示问题列表:

@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"/>
  }   
StudentService是一个类,我在其中定义了GetAllStudents、save或updateStudent方法


请帮帮我,我想你的答案很简单吧

@foreach (var item in Model) 
应该是

@foreach (var item in Model.Question_Page) 
你的模型是主页吗


你能在这里发布这个模型吗?

假设GetAllStudents返回IEnumerable 在控制器中

return View(new StudentService().GetAllStudents());
IEnumerable<Core.Model.MainPage> model = //... define function that return Enumerable MainPage
return View(model);
发送到视图的实际模型是IEnumerable

如果需要IEnumerable作为模型,只需从控制器返回适当的对象即可

return View(new StudentService().GetAllStudents());
IEnumerable<Core.Model.MainPage> model = //... define function that return Enumerable MainPage
return View(model);

如果要在不同视图上使用同一模型

    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());
    }

第一个控制器方法工作后,您需要绑定模型,第二个post方法才能工作。

尝试viewModel。具有对所需模型的引用的模型。因此,如果视图绑定到视图模型,那么视图模型中引用的所有模型也将绑定。

这实际上取决于在返回视图之前如何在控制器上构建模型。如果你能发布你的控制器,这将对你有所帮助。@t如果我已经更新了我的问题,请查看一下。顺便说一句,你想要一个特定的视图模型,与你当前用作视图模型的持久性模型无关。把它们分开,一切都会清晰明了easier@user1274646很抱歉我反应太晚了。您仍然需要清理控制器类,我看不到StudentService类的返回类型。请看下面我的答案。我尝试了这个方法,但仍然出错:主页不包含问题页面的定义