AutoMapper映射INT []或者List&LT; INT&GT;从视图模型到List&LT;类型&GT;在域模型 [英] AutoMapper mapping int[] or List<int> from ViewModel to a List<Type> in Domain Model
问题描述
我是新来AutoMapper,我一直在读,在这里读书的问题,但我不太能够弄清楚什么看起来像一个很琐碎的问题。
I'm new to AutoMapper and I've been reading and reading questions around here but I'm not quite able to figure out what looks like a very trivial question.
首先我的班,那么问题(S):
First my classes, then the question(s):
GatewayModel.cs
GatewayModel.cs
public class Gateway
{
public int GatewayID { get; set; }
public List<Category> Categories { get; set; }
public ContentType ContentType { get; set; }
// ...
}
public class Category
{
public int ID { get; set; }
public int Name { get; set; }
public Category() { }
public Category( int id ) { ID = id; }
public Category( int id, string name ) { ID = id; Name = name; }
}
public class ContentType
{
public int ID { get; set; }
public int Name { get; set; }
public ContentType() { }
public ContentType( int id ) { ID = id; }
public ContentType( int id, string name ) { ID = id; Name = name; }
}
GatewayViewModel.cs
GatewayViewModel.cs
public class GatewayViewModel
{
public int GatewayID { get; set; }
public int ContentTypeID { get; set; }
public int[] CategoryID { get; set; }
// or public List<int> CategoryID { get; set; }
// ...
}
这是我一直在阅读了一整天,这是我迄今想通了。我不知道如何从视图模型中的int [](或者如果它需要是列表)映射到模型的列表。
From what I've been reading all day this is what I have figured out so far. I don't know how to map the int[] (or List if it needs be) from the ViewModel to the List in the Model.
的Global.asax.cs
Global.asax.cs
Mapper.CreateMap<Gateway, GatewayViewModel>();
Mapper.CreateMap<GatewayViewModel, Gateway>()
.ForMember( dest => dest.ContentType, opt => opt.MapFrom( src => new ContentType( src.ContentTypeID ) ) )
.ForMember( /* NO IDEA ;) */ );
基本上,我需要从视图模型的所有INT []类别ID项目映射到列表关键词模型类型的ID属性格式。对于反向映射我需要从类别类型的所有的ID映射到我的INT [](或列表)类别ID,但我认为我有想通了(还没有得到有没有)。如果我需要做的反向映射类似的东西,请让我知道。
Basically I need to map all int[] CategoryID items from the ViewModel to the ID propery of the List Categories type in the Model. For the reverse mapping I need to map all ID's from the Category type to my int[] (or List) CategoryID but I think I have that figured out (haven't gotten there yet). If I need to do something similar for the reverse mapping, please let me know.
仅供参考,我的INT []在我的ViewModel类别ID粘合到我的视图中的SelectList。
FYI, my int[] CategoryID in my ViewModel is bonded to a SelectList in my View.
我希望codePLEX项目网站AutoMapper有一个更完整的文档,但我很高兴他们至少有他们有什么。
I wish the CodePlex project site for AutoMapper had a more complete documentation but I'm happy they at least have what they have.
感谢您!
推荐答案
您可以做到以下几点:
Mapper
.CreateMap<int, Category>()
.ForMember(
dest => dest.ID,
opt => opt.MapFrom(src => src)
);
Mapper
.CreateMap<GatewayViewModel, Gateway>()
.ForMember(
dest => dest.Categories,
opt => opt.MapFrom(src => src.CategoryID)
);
var source = new GatewayViewModel
{
CategoryID = new[] { 1, 2, 3 }
};
Gateway dst = Mapper.Map<GatewayViewModel, Gateway>(source);
显然,你不能从视图模型到模型中的名称
属性,因为它不是present映射。
Obviously you cannot map the Name
property from the view model to the model because it is not present.
这篇关于AutoMapper映射INT []或者List&LT; INT&GT;从视图模型到List&LT;类型&GT;在域模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!