【发布时间】:2012-03-06 20:03:14
【问题描述】:
配置 AutoMapper 以将 ICollection<DomainModel> 映射到 ICollection<ViewModel> 到 ICollection<object> 的最佳/最简单方法是什么?
我有一个如下所示的 DomainModel:
public class DomainModel
{
ICollection<EFEntity> Data;
//other stuff
}
我想将此 DomainModel 映射到 MVC ViewModel:
public class ViewModelWithCollection
{
ICollection<object> Data;
//other stuff
}
我需要ICollection<object>,因为我使用以下视图:
@model ViewModelWithCollection
<table>
@foreach(object x in Model.Data)
{
Html.Partial("PartialView", x)
}
</table>
对于每个具体的 ViewModel 都存在一个像这样的 PartialView:
@model ViewModel
<tr> <!-- Render specific View Data --> <tr>
当我使用时
AutoMapper.Map<DomainModel, ViewModelWithCollection>(source, target);
AutoMapper 只是做了这样的事情:
object target = (object)EFEntity
这当然行不通。
【问题讨论】:
标签: c# automapper