博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET MVC传递Model到视图的多种方式之通用方式的使用
阅读量:5117 次
发布时间:2019-06-13

本文共 7451 字,大约阅读时间需要 24 分钟。

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 List
Courses { 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     
11 ViewDataDemo12 13 66 67 68
69
70
71
72
81
82

选择课程

73 80
83
84
85

老师课程表

86
87
88
89

学生上课表

90
91
92 93

 @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     List
courses = _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 IEnumerable
2 @{ 3 Layout = null; 4 } 5 6 7 8
9
PatialViewDemo10
11
63 64 65
66
67
68
69
78
79

选择课程

70
80
81
82

老师课程表

83
84
85
86

学生上课表

87
88
89 90

 TeacherPV.cshtml与StudentPV.cshtml

1 @model IEnumerable
2
3
4
5
6
7 @foreach (var item in Model) 8 { 9
10
11
12
13 }14
编号 名称
@item.Id @item.Name

四、使用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 List
Courses { 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))

参考链接:

原文链接:

转载于:https://www.cnblogs.com/1175429393wljblog/p/11192449.html

你可能感兴趣的文章
iOS的CocoaPods(activesupport requires Ruby version >= 2.2.2)
查看>>
iOS11 automaticallyAdjustsScrollViewInsets不起作用
查看>>
MSDN文档中的一个不那么和谐的示例
查看>>
两个神奇的SQL语句
查看>>
Sublime Text 3 字体及字号大小设置
查看>>
HDOJ 4509 湫湫系列故事——减肥记II(2013腾讯编程马拉松) 并查集合并区间
查看>>
Linux wc 结合cat命令统计代码行数
查看>>
webpack4.x版本splitChunksPlugin的配置项详解与实际应用场景
查看>>
偷了世界的程序员——来自coolshell.cn
查看>>
C语言 gets()和scanf()函数的区别
查看>>
java类型转化之Hbase ImmutableBytesWritable类型转String
查看>>
Learning about the XP (extreme programming)
查看>>
centos7安装mysql(转载)
查看>>
mysql数据库查询
查看>>
模拟实现ajax加载框
查看>>
Python技能
查看>>
IntelliJ IDEA下SVN配置及使用
查看>>
JavaWeb核心之Servlet
查看>>
position / display 各个参数有什么区别
查看>>
java jdk中使用到的数据结构
查看>>