返回一个集合对象,同时这个集合的对象的属性又是一个集合对象的处理方法(ViewModel)
2015-05-25 16:45
351 查看
如果遇到需要返回一个集合对象,其中该集合中的属性又是一个集合。第一种:可在考虑用外键关联,比如在控制器中可以采用预先加载的方式加载关联的数据,比如 RoleManager.Roles.Include<x =>r.Users>.ToList();
第二种 可以考虑使用视图模型ViewModel的方式。第二种方式的适用范围更加广泛,第一种方式使用较为简单,但使用的场合有限。
一、建立视图模型 ViewModel
public class IndexRoleViewModel
{
public string Id { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "角色名称")]
public string Name { get; set; }
[Display(Name = "角色描述")]
[StringLength(50, ErrorMessage = "{0}不能超过50个字符")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public List<ApplicationUser> ApplicationUsers { get; set; } //属性为一个集合。就定义一个List泛型对象。
}
二、控制器:
public async Task<ActionResult> Index() //这里采用了异步方法,要使用异步方法,必须引入/ System.Data.Entity命名空间。
{
var rolesList =new List<IndexRoleViewModel>();
foreach (var role in await RoleManager.Roles.ToListAsync())
{
var _IndexRoleViewModel = new IndexRoleViewModel()
{
Id = role.Id,
Name = role.Name,
Description = role.Description,
ApplicationUsers =new List<ApplicationUser>() //特别注意,初始化一个空的List泛型集合。如果不初始化,将为触发一个引用为Null的异常。
};
foreach (var user in await UserManager.Users.ToListAsync()) //遍历用户
{
if (UserManager.IsInRole(user.Id,role.Name)) //如果用户拥有这个角色,就加入到viewModel 对象的List属性中。
{
_IndexRoleViewModel.ApplicationUsers.Add(user);
}
}
rolesList.Add(_IndexRoleViewModel);
}
return View(rolesList.ToList()); //异步方法在 System.Data.Entity命名空间下面。
}
三、视图:
@model IEnumerable<MajorConstruction.Areas.Admin.Models.IndexRoleViewModel> //模型为IEnumerabel<ViewModel> 类型。
@{
ViewBag.Title = "角色列表";
}
<h2>@ViewBag.Title</h2>
<hr />
<p>
@Html.ActionLink("新建角色", "Create")
</p>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>
@Html.DisplayName("角色名称")
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>@Html.DisplayName("拥有此角色的成员")</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
<ol> //使用有序列表在表格中来显示属性的多字段。
@foreach (var userForRole in item.ApplicationUsers)
{
<li>@userForRole.UserName | @userForRole.RealName</li>
}
</ol>
</td>
<td>
@Html.ActionLink("编辑", "Edit", new { id = item.Id }) |
@Html.ActionLink("详细", "Details", new { id = item.Id }) |
@Html.ActionLink("删除", "Delete", new { id = item.Id })
</td>
</tr>
}
</tbody>
</table>
第二种 可以考虑使用视图模型ViewModel的方式。第二种方式的适用范围更加广泛,第一种方式使用较为简单,但使用的场合有限。
一、建立视图模型 ViewModel
public class IndexRoleViewModel
{
public string Id { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "角色名称")]
public string Name { get; set; }
[Display(Name = "角色描述")]
[StringLength(50, ErrorMessage = "{0}不能超过50个字符")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public List<ApplicationUser> ApplicationUsers { get; set; } //属性为一个集合。就定义一个List泛型对象。
}
二、控制器:
public async Task<ActionResult> Index() //这里采用了异步方法,要使用异步方法,必须引入/ System.Data.Entity命名空间。
{
var rolesList =new List<IndexRoleViewModel>();
foreach (var role in await RoleManager.Roles.ToListAsync())
{
var _IndexRoleViewModel = new IndexRoleViewModel()
{
Id = role.Id,
Name = role.Name,
Description = role.Description,
ApplicationUsers =new List<ApplicationUser>() //特别注意,初始化一个空的List泛型集合。如果不初始化,将为触发一个引用为Null的异常。
};
foreach (var user in await UserManager.Users.ToListAsync()) //遍历用户
{
if (UserManager.IsInRole(user.Id,role.Name)) //如果用户拥有这个角色,就加入到viewModel 对象的List属性中。
{
_IndexRoleViewModel.ApplicationUsers.Add(user);
}
}
rolesList.Add(_IndexRoleViewModel);
}
return View(rolesList.ToList()); //异步方法在 System.Data.Entity命名空间下面。
}
三、视图:
@model IEnumerable<MajorConstruction.Areas.Admin.Models.IndexRoleViewModel> //模型为IEnumerabel<ViewModel> 类型。
@{
ViewBag.Title = "角色列表";
}
<h2>@ViewBag.Title</h2>
<hr />
<p>
@Html.ActionLink("新建角色", "Create")
</p>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>
@Html.DisplayName("角色名称")
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>@Html.DisplayName("拥有此角色的成员")</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
<ol> //使用有序列表在表格中来显示属性的多字段。
@foreach (var userForRole in item.ApplicationUsers)
{
<li>@userForRole.UserName | @userForRole.RealName</li>
}
</ol>
</td>
<td>
@Html.ActionLink("编辑", "Edit", new { id = item.Id }) |
@Html.ActionLink("详细", "Details", new { id = item.Id }) |
@Html.ActionLink("删除", "Delete", new { id = item.Id })
</td>
</tr>
}
</tbody>
</table>
相关文章推荐
- java面试题解惑:当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
- 当一个对象被当做参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
- 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
- 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
- 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
- java面试题:当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
- 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
- Java - 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
- 前台使用ajax 调用action方法,返回一个对象List集合的json
- spring mvc 处理方法的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void
- 写一个递归方法,传入一个Map,返回这个Map对象中一共有多个Map类型的对象 Map中是可以添加Map,被添加的这个Map也可以添加Map,所以需要判断传入的Map对象的所有子节点,如果是Map就
- 一个对象下面某一属性为集合,如何将这个集合列表展示到页面
- List集合中删除属性(一个或者多个属性)相同的对象返回List且根据对象属性进行排序输出
- 阿里巴巴 Java 开发手册:asList 的返回对象是一个 Arrays 内部类,并没有实现集合的修改方法
- 解决方法:Submit 操作失败。为类型“BomExtend”上的集合属性“BomChildrenList”传递了一个或多个关联对象,但目标集合为 null。
- (转)逐步为对象集合构建一个通用的按指定属性排序的方法
- 10_9_1编写代码,定义一个基类MyClass,其中包括虚礼方法GetString(),这个方法返回存储在受保护字段myString中的字符串,该字段可以通过只写公共属性ContainedStrin
- 逐步为对象集合构建一个通用的按指定属性排序的方法
- 写一个方法进行各种属性的更新,而不要每个界面设置改变都改变一个属性。就是点击应用或确定按钮时调用这个更新属性的方法,遍历所有(控制对象属性的)界面控件的状态(值),进行属性修改和刷新。
- 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息