如何填充Viewmodel列表集合

时间:2012-05-14 09:15:31

标签: c# asp.net-mvc asp.net-mvc-3

如何在ViewModel中填充列表集合

控制器:

  SchoolEntities db = new SchoolEntities();

    public ActionResult Index()
    {
        var student= from s in db.Students
        select s.Name.ToList();

        return View();
    }

模型视图:

     public List<Student>students { get; set;}

2 个答案:

答案 0 :(得分:0)

您需要以学生的身份返回视图,以便在您的观看中使用

 public ActionResult Index()
{
    var student= from s in db.Students
    select s.Name.ToList();

    return View(student);
}

答案 1 :(得分:0)

您可以创建ModelView,填充它并将其传递给视图,如下所示:

public ActionResult Index()
{
    var studentList = db.Students.ToList();

    return View(new ModelView { students = studentList });
}