简体   繁体   中英

Automapper Unmapped member on List mapping

Here are the Model and ViewModel

    [Table(name: "hCodeRequests")]
    public class CodeRequest
    {
        public int Id { get; set; }

        [Required]
        [StringLength(50)]
        [MaxLength(50)]
        public string Nome { get; set; }

        public ICollection<ArtigoCodeRequest> ArtigoCodeRequests { get; set; }
    }

view Model

public class CodeRequestViewModel
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public bool Selected { get; set; }
}

On my razor page OnGet handler this is what i do and it works

    [BindProperty]
    public IList<CodeRequestViewModel> CodeRequestsViewModelList { get; set; }

public async Task<IActionResult> OnGetAsync()
{
    var codeRequestModelList = _context.CodeRequests.ToList();
    CodeRequestsViewModelList = _mapper.Map<List<CodeRequest>, List<CodeRequestViewModel>>(codeRequestModelList);
}

now OnPost handler i want to get the values from VM back to my model but it does not work

var CodeRequests = _mapper.Map<List<CodeRequestViewModel>, List<CodeRequest>>(CodeRequestsViewModelList
        .Where(c => c.Selected == true).ToList());

I have tried on profile ignoring the Selected property and still gives me same exception of unmapped property

    CreateMap<CodeRequest, CodeRequestViewModel>().ForMember(dest => dest.Selected, source => source.Ignore());

我认为你需要在CreateMap方法的末尾使用ReverseMap() ,以使这个映射双向工作。

CreateMap<CodeRequest, CodeRequestViewModel>().ForMember(dest => dest.Selected, source => source.Ignore()).ReverseMap();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM