Im using AspNetBoilerPlate Core and im having a problem mapping a list of items to a viewmodel
i have this DTO in the application layer
[AutoMapFrom(typeof(LeadMeta.LeadMeta))]
public class LeadMetaDto : EntityDto
{
public int LeadId { get; set; }
public int? TenantId { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public string FriendlyKey { get; set; }
}
i want to map it to an identical viewmodel in the mvc layer, so i do the following from my controller
var lead = await _leadAppService.Get(Id);
enquiry.LeadMeta = new List<DashboardEnquiryMetaViewModel>(lead.LeadMeta);
an this is the DashboardEnquiryMetaViewModel
[AutoMapFrom(typeof(LeadMetaDto))]
public class DashboardEnquiryMetaViewModel: LeadMetaDto
{
public DashboardEnquiryMetaViewModel(LeadMetaDto output)
{
output.MapTo(this);
}
}
However my attempts are producing the following error :
INFO 2019-09-05 19:16:47,551 [9 ] ore.Mvc.Internal.ControllerActionInvoker - Executing action method PowerITCRM.Web.Controllers.LeadController.Dashboard (PowerITCRM.Web.Mvc) with arguments (13005) - Validation state: Valid ERROR 2019-09-05 19:16:48,427 [6 ] Mvc.ExceptionHandling.AbpExceptionFilter - Error mapping types.
Mapping types: IEnumerable
1 -> List
1 System.Collections.Generic.IEnumerable1[[PowerITCRM.Features.Lead.LeadMetaDto, PowerITCRM.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List
1[[PowerITCRM.Web.Models.Lead.DashboardEnquiryMetaViewModel, PowerITCRM.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] AutoMapper.AutoMapperMappingException: Error mapping types.Mapping types: IEnumerable
1 -> List
1 System.Collections.Generic.IEnumerable1[[PowerITCRM.Features.Lead.LeadMetaDto, PowerITCRM.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List
1[[PowerITCRM.Web.Models.Lead.DashboardEnquiryMetaViewModel, PowerITCRM.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] ---> System.ArgumentException: PowerITCRM.Web.Models.Lead.DashboardEnquiryMetaViewModel needs to have a constructor with 0 args or only optional args. Parameter name: type at lambda_method(Closure , IEnumerable1 , List
1 , ResolutionContext ) --- End of inner exception stack trace --- at lambda_method(Closure , IEnumerable1 , List
1 , ResolutionContext )
at AutoMapper.Mapper.AutoMapper.IMapper.Map[TSource,TDestination](TSource source, TDestination destination) in C:\projects\automapper\src\AutoMapper\Mapper.cs:line 271 at PowerITCRM.Web.Controllers.LeadController.Dashboard(Int32 Id) in C:\Users\User\Documents\Visual Studio 2017\Projects\PowerITCRM\src\PowerITCRM.Web.Mvc\Controllers\LeadController.cs:line 126 at lambda_method(Closure , Object ) at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync() INFO 2019-09-05 19:16:48,434 [6 ] ore.Mvc.Internal.ControllerActionInvoker - Executed action PowerITCRM.Web.Controllers.LeadController.Dashboard (PowerITCRM.Web.Mvc) in 898.9158ms ERROR 2019-09-05 19:16:48,618 [6
] nostics.DeveloperExceptionPageMiddleware - An unhandled exception has occurred while executing the request. AutoMapper.AutoMapperMappingException: Error mapping types.Mapping types: IEnumerable
1 -> List
1 System.Collections.Generic.IEnumerable1[[PowerITCRM.Features.Lead.LeadMetaDto, PowerITCRM.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List
1[[PowerITCRM.Web.Models.Lead.DashboardEnquiryMetaViewModel, PowerITCRM.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] ---> System.ArgumentException: PowerITCRM.Web.Models.Lead.DashboardEnquiryMetaViewModel needs to have a constructor with 0 args or only optional args. Parameter name: type at lambda_method(Closure , IEnumerable1 , List
1 , ResolutionContext ) --- End of inner exception stack trace --- at lambda_method(Closure , IEnumerable1 , List
1 , ResolutionContext )
at AutoMapper.Mapper.AutoMapper.IMapper.Map[TSource,TDestination](TSource source, TDestination destination) in C:\projects\automapper\src\AutoMapper\Mapper.cs:line 271 at PowerITCRM.Web.Controllers.LeadController.Dashboard(Int32 Id) in C:\Users\User\Documents\Visual Studio 2017\Projects\PowerITCRM\src\PowerITCRM.Web.Mvc\Controllers\LeadController.cs:line 126 at lambda_method(Closure , Object ) at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ExceptionContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at PowerITCRM.Authentication.JwtBearer.JwtTokenMiddleware.<>c__DisplayClass0_0.<b__0>d.MoveNext() in C:\Users\User\Documents\Visual Studio 2017\Projects\PowerITCRM\src\PowerITCRM.Web.Core\Authentication\JwtBearer\JwtTokenMiddleware.cs:line 22 --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) DEBUG 2019-09-05 19:16:48,713 [9 ] Abp.AspNetCore.SignalR.Hubs.AbpCommonHub - A client is disconnected: fpR1HzE9UYrEMvP_yNR28w INFO 2019-09-05 19:16:48,758 [13 ] soft.AspNetCore.Hosting.Internal.WebHost - Request finished in 4111.2398ms 101 INFO 2019-09-05 19:16:48,854 [6 ] soft.AspNetCore.Hosting.Internal.WebHost - Request finished in 1333.083ms 500 text/html; charset=utf-8
Your code should look likes this :
var lead = await _leadAppService.Get(Id);
enquiry.LeadMeta = new List<DashboardEnquiryMetaViewModel>
{
new DashboardEnquiryMetaViewModel(lead.LeadMeta)
//or lead.LeadMeta.MapTo<DashboardEnquiryMetaViewModel>();
};
This part of your code causes the exception:
enquiry.LeadMeta = new List<DashboardEnquiryMetaViewModel>(lead.LeadMeta);
You are passing the object to the constructor of the list, not the ViewModel!