C# 自动映射int[]或List<;int>;从ViewModel到列表<;类型>;域内模型

C# 自动映射int[]或List<;int>;从ViewModel到列表<;类型>;域内模型,c#,asp.net-mvc,mapping,viewmodel,automapper,C#,Asp.net Mvc,Mapping,Viewmodel,Automapper,我是AutoMapper的新手,我一直在阅读这里的问题,但我不太明白什么看起来像是一个非常琐碎的问题 首先是我的课程,然后是问题: GatewayModel.cs public class Gateway { public int GatewayID { get; set; } public List<Category> Categories { get; set; } public ContentType ContentType { get; set; }

我是AutoMapper的新手,我一直在阅读这里的问题,但我不太明白什么看起来像是一个非常琐碎的问题

首先是我的课程,然后是问题:

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; } 
}
公共类网关
{
公共int网关ID{get;set;}
公共列表类别{get;set;}
公共ContentType ContentType{get;set;}
// ...
}
公共类类别
{
公共int ID{get;set;}
公共int名称{get;set;}
公共类别(){}
公共类别(int-id){id=id;}
公共类别(int-id,字符串名){id=id;name=name;}
}
公共类ContentType
{
公共int ID{get;set;}
公共int名称{get;set;}
公共内容类型(){}
公共内容类型(int-id){id=id;}
公共内容类型(int-id,字符串名){id=id;name=name;}
}
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网关ID{get;set;}
public int ContentTypeID{get;set;}
public int[]CategoryID{get;set;}
//或公共列表类别ID{get;set;}
// ...
}
从我读了一整天的书来看,这就是我到目前为止发现的。我不知道如何将int[](或列表,如果需要)从ViewModel映射到模型中的列表

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 ;) */ );
Mapper.CreateMap();
Mapper.CreateMap()
.ForMember(dest=>dest.ContentType,opt=>opt.MapFrom(src=>newcontenttype(src.ContentTypeID)))
.ForMember(/*不知道;)*/);
基本上,我需要将ViewModel中的所有int[]CategoryID项映射到模型中List Categories类型的ID属性。对于反向映射,我需要将所有ID从Category类型映射到我的int[](或List)CategoryID,但我想我已经解决了(还没有解决)。如果我需要做一些类似的反向映射,请让我知道

仅供参考,我的视图模型中的int[]CategoryID已绑定到我视图中的SelectList

我希望AutoMapper的CodePlex项目站点有一个更完整的文档,但我很高兴他们至少有他们所拥有的


谢谢大家!

您可以执行以下操作:

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);
Mapper
.CreateMap()文件
福门博先生(
dest=>dest.ID,
opt=>opt.MapFrom(src=>src)
);
制图员
.CreateMap()文件
福门博先生(
目的地=>目的地类别,
opt=>opt.MapFrom(src=>src.CategoryID)
);
var source=新网关视图模型
{
CategoryID=new[]{1,2,3}
};
网关dst=Mapper.Map(源);

显然,您无法将
Name
属性从视图模型映射到模型,因为它不存在。

您可以执行以下操作:

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);
Mapper
.CreateMap()文件
福门博先生(
dest=>dest.ID,
opt=>opt.MapFrom(src=>src)
);
制图员
.CreateMap()文件
福门博先生(
目的地=>目的地类别,
opt=>opt.MapFrom(src=>src.CategoryID)
);
var source=新网关视图模型
{
CategoryID=new[]{1,2,3}
};
网关dst=Mapper.Map(源);
显然,您无法将
Name
属性从视图模型映射到模型,因为它不存在