这是Model和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; }
}
视图模型
public class CodeRequestViewModel
{
public int Id { get; set; }
public string Nome { get; set; }
public bool Selected { get; set; }
}
在我的剃须刀页面OnGet处理程序上,这就是我所做的并且有效
[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);
}
现在OnPost处理程序,我想将VM中的值返回到我的模型,但是它不起作用
var CodeRequests = _mapper.Map<List<CodeRequestViewModel>, List<CodeRequest>>(CodeRequestsViewModelList
.Where(c => c.Selected == true).ToList());
我尝试忽略配置文件属性,但仍然给我未映射属性的例外情况
CreateMap<CodeRequest, CodeRequestViewModel>().ForMember(dest => dest.Selected, source => source.Ignore());
答案 0 :(得分:0)
我认为您需要在ReverseMap()
方法的末尾使用CreateMap
,以使此映射双向工作。
CreateMap<CodeRequest, CodeRequestViewModel>().ForMember(dest => dest.Selected, source => source.Ignore()).ReverseMap();