3

I'm new to backend development, having some trouble mapping a viewmodel to dto that has a list.

Can you help me figure out whats wrong with the mapper. The result is coming in correct from the dto. I have a list of 7 items. When it maps to the view they are gone.

Here is the viewmodel

public class StatisticsViewModel : BaseViewModel
{
    public string StartDate { get; set; }
    public string EndDate { get; set; }
    public string ProviderId { get; set; }
    public List<StatisticsTotalsViewModel> Totals { get; set; } = new List<StatisticsTotalsViewModel>();
    public List<StatisticsProvidersViewModel> Providers { get; set; } = new List<StatisticsProvidersViewModel>();
}

public class StatisticsTotalsViewModel
{
    public string PayerName { get; set; }
    public string PayerType { get; set; }
    public short Status { get; set; }
    public int TotalCount { get; set; }
    public decimal TotalBalance { get; set; }
}

Heres the dto

public class StatisticsDto
{
    public string StartDate { get; set; }
    public string EndDate { get; set; }
    public string ProviderId { get; set; }
    public List<StatisticsTotalsDto> Totals { get; set; } = new List<StatisticsTotalsDto>();
    public List<StatisticsProvidersDto> Providers { get; set; } = new List<StatisticsProvidersDto>();
}

public class StatisticsTotalsDto
{
    public string PayerName { get; set; }
    public string PayerType { get; set; }
    public short Status { get; set; }
    public int TotalCount { get; set; }
    public decimal TotalBalance { get; set; }
}

Here's the mapper

    public static StatisticsViewModel MapToView(StatisticsDto dto)
    {
        var viewmodel = new StatisticsViewModel();

        viewmodel.StartDate = dto.StartDate;
        viewmodel.EndDate = dto.EndDate;
        viewmodel.ProviderId = dto.ProviderId;

        var dtoTotals = new List<StatisticsTotalsDto>();
        var totals = new List<StatisticsTotalsViewModel>();

        foreach (var item in dtoTotals)
        {
            var totalsModel = new StatisticsTotalsViewModel();
            item.PayerName = totalsModel.PayerName;
            item.PayerType = totalsModel.PayerType;
            item.Status = totalsModel.Status;
            item.TotalBalance = totalsModel.TotalBalance;
            item.TotalCount = totalsModel.TotalCount;
            totals.Add(totalsModel);
        }

        viewmodel.Totals = totals;

       return viewmodel;

    }
stanggt3
  • 63
  • 4
  • 2
    What is `PayerClaimStatisticsViewModel`? And your just looping through `dtoTotals` which you initialize as an empty collection. And your method returns `StatisticsViewModel` but contains no `return` statement –  Apr 25 '17 at 22:21
  • Thank you! One was a typo but you got me pointed in the right direction – stanggt3 Apr 25 '17 at 22:29
  • 2
    The I assume what you want is `foreach (var item in dto.Totals)` –  Apr 25 '17 at 22:31
  • You should also check out Automapper. It's pretty much the accepted standard for mapping like this. – The Muffin Man Apr 25 '17 at 22:31
  • I checked out AutoMapper but couldn't find much help with the new version. As Im still learning I need some good examples and couldnt find them – stanggt3 Apr 25 '17 at 22:35

2 Answers2

1

Problem in this line. Instead

var dtoTotals = new List<StatisticsTotalsDto>();

You need to receive list of StatisticsTotalsDto, instead of create new empty list

var dtoTotals = dto.Totals;
Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36
-1

You can try Automapper.

Say for example:

public static StatisticsViewModel MapToView(StatisticsDto dto)
{
     Mapper.Initialize(cfg => cfg.CreateMap<StatisticsDto, StatisticsViewModel>());
     var ViewModel = Mapper.Map<StatisticsViewModel>(dto);

     return viewModel;
}

Take a look at here to know more about Automapper. You can also check here and here if you face problem mapping list items.

P.S Don't forget to include Automapper to your project and add using Automapper() at the top where you are using it

Community
  • 1
  • 1
Adnan Niloy
  • 469
  • 11
  • 18