我想要映射以下类的两个集合:
public class Client_Crime_ViewModel
{
public Client_Crime_ViewModel() { }
public Client_Crime_ViewModel(CrimeIncident ci)
{
Id = ci.Id;
CaseNumber = ci.CaseNumber;
DateOfIncident = ci.DateOfIncident;
Description = ci.Description;
}
public Int64 Id
{
get;
set;
}
[Required]
[DisplayName("Case Number")]
public string CaseNumber { get; set; }
[DataType(DataType.Date)]
[Required]
[DisplayName("Date of Incident")]
public string DateOfIncident { get; set; }
[DataType(DataType.MultilineText)]
[Required]
public string Description { get; set; }
}
public class CrimeIncident
{
public Int64 Id { get; set; }
[Required]
public string CaseNumber { get; set; }
[Required]
public string Description { get; set; }
[DataType(DataType.Date)]
public string DateOfIncident { get; set; }
public CrimeIncident() { }
public CrimeIncident(string caseNumber, string dateOfIncident, string description)
{
CaseNumber = caseNumber;
Description = description;
DateOfIncident = dateOfIncident;
}
}
我尝试过以下两种方式进行映射:
方法1:
Mapper.CreateMap<List<Client_Crime_ViewModel>, List<CrimeIncident>>();
List<Client_Crime_ViewModel> cvmList = System.Web.Helpers.Json.Decode<System.Collections.Generic.List<Client_Crime_ViewModel>>(rb.Form["CrimeCollection"]);
List<CrimeIncident> ciList = Mapper.Map<List<Client_Crime_ViewModel>, List<CrimeIncident>>(cvmList);
方法2:
Mapper.CreateMap<List<Client_Crime_ViewModel>, List<CrimeIncident>>();
List<Client_Crime_ViewModel> cvmList = System.Web.Helpers.Json.Decode<System.Collections.Generic.List<Client_Crime_ViewModel>>(rb.Form["CrimeCollection"]);
List<CrimeIncident> ciList = Mapper.Map<List<CrimeIncident>>(cvmList);
两种方法都失败了。调试显示,虽然cvmList
的元素数为> 0
,但ciList的元素数仍为0
。
知道我做错了什么吗? 提前谢谢。
答案 0 :(得分:4)
您无需为列表创建地图,只需映射实体。