MVC控制器向视图传递数据包含多个实体类的解决方案有很多,这里主要针对视图模型、动态模型以及Tuple三种方法进行一些总结与记录。
基础集合类:TableA
namespace ViewModelStudy.Models{ ???public class TableA ???{ ???????public int A { get; set; } ???????public int B { get; set; } ???????public int C { get; set; } ???}}
基础集合类:TableB
namespace ViewModelStudy.Models{ ???public class TableB ???{ ???????public int X { get; set; } ???????public int Y { get; set; } ???????public int Z { get; set; } ???}}
构建分别以TableA,TableB为基础的集合
public List<TableA> tableA() ???????{ ???????????var table = new List<TableA>() ???????????{ ???????????????new TableA{A=1,B=2,C=3}, ???????????????new TableA{A=4,B=5,C=6} ???????????}; ???????????return table; ???????} public List<TableB> tableB() ???????{ ???????????var table = new List<TableB>() ???????????{ ???????????????new TableB{X=1,Y=2,Z=3}, ???????????????new TableB{X=4,Y=5,Z=6} ???????????}; ???????????return table; ???????}
方法一:新建ViewModel向视图传递集合数据
using System.Collections.Generic;namespace ViewModelStudy.Models{ ???public class ViewTable ???{ ???????public List<TableA> TableA { get; set; } ???????public List<TableB> TableB { get; set; } ???}}
public ActionResult ViewModel() ???????{ ???????????var ViewTable = new ViewTable() ???????????{ ???????????????TableA = tableA(), ???????????????TableB = tableB() ???????????}; ???????????return View(ViewTable); ???????}
@using ViewModelStudy.Models@model ViewTable@{ ???Layout = null;}<!DOCTYPE html><html><head> ???<title>Index</title></head><body> ???<div> ???<table class="table1"> ???????<tbody> ???????@foreach (var item in Model.TableA) ???????{ ???????????<tr> ???????????????<td>@item.A</td> ???????????????<td>@item.B</td> ???????????????<td>@item.C</td> ???????????</tr> ???????} ???????</tbody> ???</table> ???<table class="table2"> ???????<tbody> ???????@foreach (var item in Model.TableB) ???????{ ???????????<tr> ???????????????<td>@item.X</td> ???????????????<td>@item.Y</td> ???????????????<td>@item.Z</td> ???????????</tr> ???????} ???????</tbody> ???</table> ???</div></body></html>
方法二:使用dynamic传递数据
???????public ActionResult ExpandoObject() ???????{ ???????????dynamic table = new ExpandoObject(); ???????????table.TableA = tableA(); ???????????table.TableB = tableB(); ???????????return View(table); ???????}
@model dynamic@{ ???Layout = null;}<!DOCTYPE html><html><head> ???<title>Test</title></head><body> ???<div> ???????<table class="table1"> ???????<tbody> ???????@foreach (var item in Model.TableA) ???????{ ???????????<tr> ???????????????<td>@item.A</td> ???????????????<td>@item.B</td> ???????????????<td>@item.C</td> ???????????</tr> ???????} ???????</tbody> ???</table> ???<table class="table2"> ???????<tbody> ???????@foreach (var item in Model.TableB) ???????{ ???????????<tr> ???????????????<td>@item.X</td> ???????????????<td>@item.Y</td> ???????????????<td>@item.Z</td> ???????????</tr> ???????} ???????</tbody> ???</table> ???</div></body></html>
方法三:使用Tuple传递数据
???????public ActionResult Tuple() ???????{ ???????????var table1 = tableA(); ???????????var table2 = tableB(); ???????????var TupleModel = new Tuple<List<TableA>, List<TableB>>(table1, table2); ???????????return View(TupleModel); ???????}
@using ViewModelStudy.Models;@model Tuple<List<TableA>,List<TableB>>@{ ???Layout = null;}<!DOCTYPE html><html><head> ???<title>Tuple</title></head><body> ???<div> ???????<table class="table1"> ???????<tbody> ???????@foreach (var item in Model.Item1) ???????{ ???????????<tr> ???????????????<td>@item.A</td> ???????????????<td>@item.B</td> ???????????????<td>@item.C</td> ???????????</tr> ???????} ???????</tbody> ???</table> ???<h1>xxxxxxxxxxxxxxxxxxx</h1> ???<table class="table2"> ???????<tbody> ???????@foreach (var item in Model.Item2) ???????{ ???????????<tr> ???????????????<td>@item.X</td> ???????????????<td>@item.Y</td> ???????????????<td>@item.Z</td> ???????????</tr> ???????} ???????</tbody> ???</table> ???</div></body></html>
总结
使用新建视图模型优点在于对于较为复杂集合展示数据时,使用强类型能够较方便找到集合下面的实体属性,而缺点在于需要新建实体类,可能有相当一部分人都不喜欢新建实体类。
使用动态类型和新疆视图模型相比,优势在于不需要新建实体类,想怎么命名就怎么命名,缺点也是由此而来,没法动态推断出集合下的实体类属性,可能对于集合属性比较复杂的页面来说单单敲出这些属性就是一个很大的问题。
Tuple传递数据是我比较喜欢的一种方式,你只需要记住该集合中各部分数据的序号即可,而且对于实体类可以动态给出其包含的属性。
原文:http://www.cnblogs.com/fuxuyang/p/7988813.html
MVC控制器传递多个实体类集合到视图的方案总结
原文地址:https://www.cnblogs.com/hnsongbiao/p/8728263.html