0

如何在 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;}
4

2 回答 2

0

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

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

    return View(student);
}
于 2012-05-14T09:17:51.530 回答
0

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

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

    return View(new ModelView { students = studentList });
}
于 2012-05-14T09:29:52.283 回答