简体   繁体   English

自动映射器无法转换通用列表

[英]Automapper fails to translate generic lists

I have two collections of the following classes that I want to map: 我有两个要映射的以下类的集合:

 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;
        }
    }

I have tried mapping in both of the following ways: 我尝试通过以下两种方式进行映射:

Method 1: 方法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);

Method 2: 方法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);

Both methods fail. 两种方法均失败。 Debugging reveals that although the element count for cvmList is > 0 , the element count for ciList remains at 0 . 调试显示,尽管cvmList的元素计数> 0 ,但ciList的元素计数仍为0

Any idea what I am doing wrong? 知道我在做什么错吗? Thanks in advance. 提前致谢。

您无需为列表创建映射,只需映射实体即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM