Domanda

I am trying to get some data using one GetData. This business method is called from the controller through a service layer in this Action method:

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);
}

I get this error:

Cannot implicitly convert type 'System.Collections.Generic.List<BusinessService.Report.MeanData>' to 'DomainModels.Reports.MeanData'

A colleague had suggested using Automapper for this, so I changed the Action method like this, based on what worked for him:

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);
}

I get this error:

The best overloaded method match for 'AutoMapper.Mapper.Map<DomainModels.Reports.MeanData,BusinessService.Report.MeanData>(DomainModels.Reports.MeanData)' has some invalid arguments

The DomainModel entity:

[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; }
}

The BusinessService entity, which can be found in the generated reference.cs is having properties with same names as the DomainModel entity.

What am I doing wrong in both instances?

È stato utile?

Soluzione

Your reports client returns list of business entities and you are trying to map them to single entity. I think you should map collection of business entities to collection of view models (currently you are trying to map collection to single view model):

using (var reportsClient = new ReportsClient())
{
    List<BusinessService.Report.MeanData> model = 
        reportsClient.GetData(reportType, toDate, fromDate); 
    IEnumerable<DomainModels.Reports.MeanData> viewModel = 
        Mapper.Map<IEnumerable<DomainModels.Reports.MeanData>>(model);
}

return PartialView("_Grid", viewModel);

Move mapping creation to application start:

Mapper.CreateMap<DomainModels.Reports.MeanData, BusinessService.Report.MeanData>();

Also consider to use aliases if you have types with same name:

using BusinessMeanData = BusinessService.Reports.MeanData;
using MeanDataViewModel = DomainModel.Reports.MeanData;

Or (better) add ViewModel suffix to name of type which acts as view model. In this case code will look like:

using (var reportsClient = new ReportsClient())
{
    var model = reportsClient.GetData(reportType, toDate, fromDate); 
    var viewModel = Mapper.Map<IEnumerable<MeanDataViewModel>>(model);
}

return PartialView("_Grid", viewModel);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top