如何自动映射这个(映射子成员) [英] How to automap this(mapping sub members)

查看:80
本文介绍了如何自动映射这个(映射子成员)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情

public class ProductViewModel
{
  public int SelectedProductId { get; set; }
  public string ProductName {get; set;}
  public int Qty {get; set;}
   public List<SelectListItem> Products { get; set}; 
}

我有这样一个域

public class Product
{
  public int ProductId {get; set;}
  public string ProductName {get; set;}
  public int Qty {get; set;}
}


public class Store
{
  public Product() {get; set;}
}

现在我需要做的映射。

//在我的控制器

var result = Mapper.Map<ProductViewModel, Store>(Product);

,因为它无法弄清楚如何把产品编号中,因为这将不绑定任何东西

this won't bind anything since it can't figure out how to put the ProductId in since it is

Store.Product.ProductId;

我的地图是这样的。

My map is like this

Mapper.CreateMap<ProductViewModel, Store>().ForMember(dest => dest.Product.ProductId, opt => opt.MapFrom(src => src.SelectedProductId));

我得到这个错误

防爆pression'DEST =>
  转换(dest.Product.SelectedProductId必须
  解析到顶级成员。参数
  名称:lambdaEx pression

Expression 'dest => Convert(dest.Product.SelectedProductId' must resolve to top-level member. Parameter name: lambdaExpression

我不确定如何做到这一点。

I am unsure how to do this.

推荐答案

你得到这个错误是因为你不能声明映射声明在你的对象图深多个级别。

The error your getting is because you cannot declare mapping declarations more than one level deep in your object graph.

由于你只发布一个属性的我很难给你codeS,这将使这项工作。一种选择是改变你的视图模型属性MyTestTestId和惯例会自动拿起这一点。

Because you've only posted one property its hard for me to give you the codes that will make this work. One option is to change your viewmodel property to MyTestTestId and the conventions will automatically pick up on that.

这篇关于如何自动映射这个(映射子成员)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆