【问题标题】:Automapper mapping issue: flattening from DTO to ViewModel works - doesn't work the other way aroundAutomapper 映射问题:从 DTO 展平到 ViewModel 有效 - 反之则无效
【发布时间】:2011-06-15 17:32:12
【问题描述】:

我的 DTO(为演示目的而简化):

Item(映射到我的相关 ViewModel 的 DTO):

public class Item {
    public Item() { }
    public virtual Guid ID { get; set; }
    public virtual ItemType ItemType { get; set; }
    public virtual string Title { get; set; }
}

ItemType(由我的 Item 类引用):

public class ItemType {
    public ItemType() { }
    public virtual Guid ID { get; set; }
    public virtual IList<Item> Items { get; set; }
    public virtual string Name { get; set; }
}

我的 ViewModel(用于编辑我的 Item 类数据):

public class ItemEditViewModel {
    public ItemEditViewModel () { }
    public Guid ID { get; set; }
    public Guid ItemTypeID { get; set; }
    public string Title { get; set; }
    public SelectList ItemTypes { get; set; }
    public IEnumerable<ItemType> ItemTypeEntities { get; set; }

    public BuildItemTypesSelectList(Guid? itemTypeID)
    {
        ItemTypes = new SelectList(ItemTypeEntities, "ID", "Name", itemTypeID);
    }
}

我的 AutoMapper 映射代码:

Mapper.CreateMap<Item, ItemEditViewModel>()
    .ForMember(dest => dest.ItemTypes, opt => opt.Ignore());
Mapper.CreateMap<ItemEditViewModel, Item>();

控制器代码(再次,为演示而简化):

public ActionResult Create()
{
    var itemVM = new ItemEditViewModel();
    // Populates the ItemTypeEntities and ItemTypes properties in the ViewModel:
    PopulateEditViewModelWithItemTypes(itemVM, null);
    return View(itemVM);
}

[HttpPost]
public ActionResult Create(ItemEditViewModel itemVM)
{
    if (ModelState.IsValid) {
        Item newItem = new Item();
        AutoMapper.Mapper.Map(itemVM, newItem);
        newItem.ID = Guid.NewGuid();
        ...
        // Validation and saving code here...
        ...
        return RedirectToAction("Index");
    }

    PopulateEditViewModelWithItemTypes(itemVM, null);
    return View(itemVM);
}

现在,发生了什么事:

在我的控制器中的 HttpPost Create 操作结果中,我使用 Automapper 将我的 ItemEditViewModel 映射到我的 Item DTO 类,在 SelectList 中选择的 ItemType ID 值不会绑定到 Item.ItemType.ID 属性。 Item.ItemType 属性为空。

我认为这是因为,因为我的 Item DTO 类中没有 ItemTypeID Guid 值,并且我没有为我的 Item DTO 中的同名属性创建新的 ItemType 类,所以 AutoMapper 无法存储 ItemType ID 值。

我认为这取决于我的 Automapper 映射配置。

我确定我忽略了一些简单的事情。

提前感谢您的任何建议!

【问题讨论】:

    标签: asp.net-mvc automapper


    【解决方案1】:

    很确定这是因为 automapper 被设计为 Big Shape-> Smaller/Flat Shape 映射工具,而不是相反。这只是不受支持。

    【讨论】:

    • 这令人失望。似乎,在我的 ViewModel 到 DTO 的映射配置中,我能够为 ItemType 类指定一个构造函数,将我从 ViewModel 中选择的 ItemTypeID 绑定到该构造函数。我已经有一个 ItemType 的构造函数,它接受 ID 的 Guid 参数。现在,我已经修改了我的 Create HttpPost ActionResult 方法,通过使用构造函数将 DTO 的 ItemType 属性设置为新的 ItemType 对象:newItem.ItemType = new ItemType(itemVM.ItemTypeID);
    【解决方案2】:

    你应该可以说:

    Mapper.CreateMap<ItemEditViewModel, Item>()
        .ForMember(dest => dest.ItemType, opt => opt.MapFrom(src =>
            {
                return new ItemType()
                    {
                        ID = src.ItemTypeID
                    }
            };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多