我正在尝试使用一个来获取一些数据GetData
。这个业务方法是通过这个 Action 方法中的一个服务层从控制器调用的:
public PartialViewResult Grid()
{
var model = new DomainModels.Reports.MeanData();
using (var reportsClient = new ReportsClient())
{
model = reportsClient.GetData(reportType, toDate, fromDate); //<= error on this line
}
return PartialView("_Grid", model);
}
我收到此错误:
无法将类型“
System.Collections.Generic.List<BusinessService.Report.MeanData>
”隐式转换为“DomainModels.Reports.MeanData
”
一位同事建议为此使用 Automapper,所以我根据对他有用的方法更改了这样的 Action 方法:
public PartialViewResult Grid()
{
using (var reportsClient = new ReportsClient())
{
Mapper.CreateMap<DomainModels.Reports.MeanData, BusinessService.Report.MeanData>();
var model = reportsClient.GetData(reportType, toDate, fromDate);
DomainModels.Reports.MeanData viewModel = //<= error on this line
Mapper.Map<DomainModels.Reports.MeanData, BusinessService.Report.MeanData>(model);
}
return PartialView("_Grid", viewModel);
}
我收到此错误:
AutoMapper.Mapper.Map<DomainModels.Reports.MeanData,BusinessService.Report.MeanData>
' (DomainModels.Reports.MeanData)'的最佳重载方法匹配有一些无效参数
DomainModel 实体:
[DataContract]
public class MeanData
{
[DataMember]
public string Description { get; set; }
[DataMember]
public string Month3Value { get; set; }
[DataMember]
public string Month2Value { get; set; }
[DataMember]
public string Month1Value { get; set; }
}
可以在生成的 BusinessService 实体中找到reference.cs
与 DomainModel 实体同名的属性。
在这两种情况下我做错了什么?