ASP.NET MVC3.0中同一View如何返回多个Model或数据集

时间:2022-11-28 20:11:06

在控制器里定义一个Dictionary 泛型类,再把要用到的不同模型加进去

 controller:

    public ActionResult Index() {   


IDictionary<string, object> test = new Dictionary<string, object>();
test.Add("StudentList", new List<StudentInfo>());
return View(test);
}

 view视图:

@model IDictionary<string, object> 
@{Layout = null; }
<!DOCTYPE html>
<html>
<head>
<title>我的个人主页</title>
</head>
<body>
<ul style="list-style: none; margin-left: 10px;">
@foreach (var m in (IList<EnglishDiary.Models.StudentInfo>)Model["StudentList"]) {
<li>@m.StuID</li>
<li>@m.StuName</li>
}
</ul>
</body>
</html>