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