AutoMapper:地图IList的模型来IEnumerable的视图模型中 [英] AutoMapper: Map IList in model to IEnumerable in viewModel
本文介绍了AutoMapper:地图IList的模型来IEnumerable的视图模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能在集团GroupViewModel映射到IList中的IEnumerable
型号
公共类组
{
公共字符串名称{;组; }
公共虚拟的IList<&许可GT;权限
{
{返回_permissoes? (_permissions =新的List<&许可GT;()); }
集合{Permissoes.Clear(); _permissions =价值; }
}
}公共类权限
{
众长标识{搞定;组; }
公共字符串名称{;组; }
}
视图模型
公共类GroupViewModel
{
公共字符串名称{;组; }
公共IEnumerable的< PermissionViewModel>权限{搞定;组; }
}公共类PermissionViewModel
{
公众诠释标识{搞定;组; }
公共字符串名称{;组; }
}
解决方案
就这么简单:
VAR GRP =新的组{权限=新的List<&许可GT; {新的许可{名称=1权限},
{新的权限{名称=2权限}}}};Mapper.CreateMap<许可,PermissionViewModel>();Mapper.CreateMap<集团,GroupViewModel>();
VAR的结果= Mapper.Map< GroupViewModel>(GRP);
Is it possible to map IList in Group to IEnumerable in GroupViewModel?
Model
public class Group
{
public string Name { get; set; }
public virtual IList<Permission> Permissions
{
get { return _permissoes ?? (_permissions = new List<Permission>()); }
set { Permissoes.Clear(); _permissions = value; }
}
}
public class Permission
{
public long Id { get; set; }
public string Name { get; set; }
}
ViewModel
public class GroupViewModel
{
public string Name { get; set; }
public IEnumerable<PermissionViewModel> Permissions { get; set; }
}
public class PermissionViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
解决方案
Simple as that:
var grp = new Group {Permissions = new List<Permission>{new Permission{Name="Permission 1"},
{new Permission{Name="Permission 2"}}}};
Mapper.CreateMap<Permission, PermissionViewModel>();
Mapper.CreateMap<Group, GroupViewModel>();
var result = Mapper.Map<GroupViewModel>(grp);
这篇关于AutoMapper:地图IList的模型来IEnumerable的视图模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文