[英]AutoMapper doesn't map ViewModel to Model
I have ViewModel: 我有ViewModel:
public class EmployeeViewModel
{
public Developer Developer { get; set; }
public Administrator Administrator { get; set; }
}
and Model: 和型号:
public class Developer
{
//some stuff
}
I'm trying to map it in the Action: 我正在尝试在Action中映射它:
public ActionResult AddDeveloper(EmployeeViewModel employee)
{
AutoMapper.Mapper.CreateMap<EmployeeViewModel, Developer>();
Developer developer = AutoMapper.Mapper.Map<EmployeeViewModel, Developer>(employee);
.......
}
after that developer
variable has no values for parameters that employee
has. 之后,该
developer
变量没有employee
具有的参数值。 Could you please help me? 请你帮助我好吗?
You are simply trying to map a class to a property effectively. 您只是想有效地将类映射到属性。 You need to explicitly tell how to bring both element onto the same level, for example:
您需要明确说明如何将两个元素都置于同一级别,例如:
public ActionResult AddDeveloper(EmployeeViewModel employee)
{
AutoMapper.Mapper.CreateMap<EmployeeViewModel, Developer>()
.ForMember(destination => destination, option => option.MapFrom(source => source.Developer));
Developer developer = AutoMapper.Mapper.Map<EmployeeViewModel, Developer>(employee);
}
Now the same thing should work. 现在,同一件事应该起作用。 I have never tried doing the mapping at that level, I suspect you may have to try using the "AfterMap" function like here: https://stackoverflow.com/a/4750709/3002953 .
我从未尝试过在该级别进行映射,我怀疑您可能必须尝试使用“ AfterMap”功能,例如: https ://stackoverflow.com/a/4750709/3002953。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.