ASP.NET MVC传递Model到视图的多种方式总结——通用方式的使用
有多种方式可以将数据传递到视图,如下所示:
ViewData
ViewBag
PartialView
TempData
ViewModel
Tuple
场景:
在视图页面,下拉框选择课程触发事件,分别显示老师课程表、学生上课表,如图:
相关的Model:
1 public class Course 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 } 6 7 public class Teacher 8 { 9 public int Id { get; set; }10 public string Name { get; set; }11 public ListCourses { get; set; }12 }13 14 public class Student15 {16 public int Id { get; set; }17 public string Name { get; set; }18 public List Courses { get; set; }19 }
一、使用ViewData传递多个Model
HomeController
1 public ActionResult Index()2 {3 ViewData["Courses"] = _repository.GetCourses();4 ViewData["Students"] = _repository.GetStudents();5 ViewData["Teachers"] = _repository.GetTeachers();6 return View();7 }
Home/Index.cshtml
1 @using MvcApplication1.Models 2 @using System.Web.Helpers; 3 @{ 4 Layout = null; 5 } 6 7 8 9 10 11ViewDataDemo 12 13 66 67 68698470
8371 8272 选择课程
73 80 818588老师课程表
86 878992 93学生上课表
90 91
@Html.Raw(Json.Encode(ViewData["Students"]))是把Model转换成json字符串,需要用到System.Web.Helpers,把此类库引用到项目,并且必须设置"复制到本地属性"为true,否则报错。
二、使用ViewBag传递多个Model
HomeController
1 public ActionResult ViewBagDemo()2 {3 ViewBag.Courses = _repository.GetCourses();4 ViewBag.Students = _repository.GetStudents();5 ViewBag.Teachers = _repository.GetTeachers();6 return View();7 }
Home/ViewBagDemo.cshtml
下拉框遍历课程改成:
@foreach (var course in ViewBag.Courses)getTeacherTable()方法中改成:
var teachers = @Html.Raw(Json.Encode(ViewBag.Teachers));getStudentTable()方法中改成:
var students = @Html.Raw(Json.Encode(ViewBag.Students));
三、使用部分视图传递多个Model
HomeController
1 public ActionResult PartialViewDemo() 2 { 3 Listcourses = _repository.GetCourses(); 4 return View(courses); 5 } 6 7 public ActionResult StudentsToPVDemo(string courseName) 8 { 9 IEnumerable courses = _repository.GetCourses();10 var selectedCourseId = (from c in courses11 where c.Name == courseName12 select c.Id).FirstOrDefault();13 IEnumerable students = _repository.GetStudents();14 var studentsInCourse = students.Where(s => s.Courses.Any(c => c.Id == selectedCourseId)).ToList();15 return PartialView("StudentPV", studentsInCourse);16 }17 18 public ActionResult TeachersToPVDemo(string courseName)19 {20 IEnumerable courses = _repository.GetCourses();21 var selectedCourseId = (from c in courses22 where c.Name == courseName23 select c.Id).FirstOrDefault();24 IEnumerable teachers = _repository.GetTeachers();25 var teachersForCourse = teachers.Where(t => t.Courses.Any(c => c.Id == selectedCourseId)).ToList();26 return PartialView("TeacherPV", teachersForCourse);27 }
Home/PartialViewDemo.cshmtl
1 @model IEnumerable2 @{ 3 Layout = null; 4 } 5 6 7 8 9 PatialViewDemo 10 11 63 64 65668167
8068 7969 选择课程
70 788285老师课程表
83 848689 90学生上课表
87 88
TeacherPV.cshtml与StudentPV.cshtml
1 @model IEnumerable2 3
4 7 @foreach (var item in Model) 8 { 9编号 5名称 610 13 }14@item.Id 11@item.Name 12
四、使用TempData传递多个Model
HomeController
1 public ActionResult TempDataDemo() 2 { 3 //第一次从数据库读取数据放到TempData中,以后的请求从TempData中获取数据 4 TempData["Courses"] = _repository.GetCourses(); 5 //让TempData保存数据,直到下一次请求 6 TempData.Keep("Courses"); 7 return View(); 8 } 9 10 public ActionResult TeachersTempData(string courseName)11 {12 var courses = TempData["Courses"] as IEnumerable;13 //由于TempData["Courses"]还要被下一次请求,继续TempData保存数据14 TempData.Keep("Courses");15 var selectedCourseId = (from c in courses16 where c.Name == courseName17 select c.Id).FirstOrDefault();18 IEnumerable teachers = _repository.GetTeachers();19 var teachersForCourse = teachers.Where(t => t.Courses.Any(c => c.Id == selectedCourseId)).ToList();20 return PartialView("TeacherPV", teachersForCourse);21 }22 23 public ActionResult StudentsTempData(string courseName)24 {25 var courses = TempData["Courses"] as IEnumerable ;26 //由于TempData["Courses"]还要被下一次请求,继续TempData保存数据27 TempData.Keep("Courses");28 var selectedCourseId = (from c in courses29 where c.Name == courseName30 select c.Id).FirstOrDefault();31 IEnumerable students = _repository.GetStudents();32 var studentsForCourse = students.Where(s => s.Courses.Any(c => c.Id == selectedCourseId)).ToList();33 return PartialView("StudentPV", studentsForCourse);34 }
Home/TempDataDemo.cshtml
下拉框遍历课程:
@foreach (var course in Model)ajax请求老师课程表:
@Url.Action("TeachersTempData","Home")ajax请求学生上课表:
@Url.Action("StudentsTempData","Home")
五、使用ViewModel传递多个Model
View Model
1 public class SchoolVm2 {3 public ListCourses { get; set; }4 public List Students { get; set; }5 public List Teachers { get; set; }6 }
HomeController
1 public ActionResult ViewModelDemoVM()2 {3 SchoolVm vm = new SchoolVm();4 vm.Courses = _repository.GetCourses();5 vm.Teachers = _repository.GetTeachers();6 vm.Students = _repository.GetStudents();7 return View(vm);8 }
Home/ViewModelDemoVM.cshtml
@model MvcApplication1.Models.SchoolVm
下拉框遍历课程:
@foreach (var course in Model.Courses)ajax请求老师课程表和学生上课表:
@Html.Raw(Json.Encode(Model.Teachers)) @Html.Raw(Json.Encode(Model.Students))
六、使用Tuple传递多个Model
HomeController
1 public ActionResult TupleDemo()2 {3 var allModels = new Tuple
, List , List >(_repository.GetCourses(),4 _repository.GetTeachers(), _repository.GetStudents()) {};5 return View(allModels);6 }
Home/TupleDemo.cshtml
@model Tuple <List <MvcApplication1.Models.Course>, List <MvcApplication1.Models.Teacher>, List <MvcApplication1.Models.Student>>
下拉框遍历课程:
@foreach (var course in Model.Item1)ajax请求老师课程表和学生上课表:
@Html.Raw(Json.Encode(Model.Item2)) @Html.Raw(Json.Encode(Model.Item3))参考链接:
原文链接: