简体   繁体   English

C#MVC3 Automapper类别包含清单

[英]C# MVC3 Automapper class containing list

I´m building an automapper with the following structures: 我正在构建具有以下结构的自动映射器:

Domain: 域:

public class EnterpriseInfo
{
    public string Name { get; set; }
    public string Description { get; set; }
    public bool Installed { get; set; }
    public bool Enabled { get; set; }
    public List<Msg> MsgList { get; set; }

    ...some methods...
}


public class Msg
{ 
    public DateTime DateTime { get; set; }
    public string From { get; set; }
    public string Message { get; set; }
 }

ViewModel classes: ViewModel类:

public class HomeLogonHomeViewModel
{
    public string CompanyName;
    public List<SysMsg> MsgList;
}

   public class SysMsg 
   {   
        public DateTime DateTime { get; set; }
        public string From { get; set; }
        public string Message { get; set; }
   }

In the view I need to show something like: 在视图中,我需要显示以下内容:

Messages for company: BALBALBALBA

12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon
12/12/12 00:00 From: AAA Message: Nnonononon

I´m in trouble configuring the automapper, as I have a sigle field with a single field and a list with another list. 我在配置自动映射器时遇到了麻烦,因为我有一个包含单个字段的sigle字段和一个包含另一个列表的列表。 Can someone please help me... 有人可以帮帮我吗...

Also, I may ask if this does indeed needs an Automapper.... 另外,我可能会问这是否确实需要Automapper。

[EDIT] [编辑]

Working code: 工作代码:

    Mapper.CreateMap<EnterpriseInfo, HomeLogonHomeViewModel>()
        .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => src.Name));

There is not need to explicit Msg to SysMsg if it was changed to be the same name. 如果已将SysMsg更改为相同的名称,则无需向SysMsg显式显示Msg。

You need to map EnterpriseInfo to HomeLogonHomeViewModel. 您需要将EnterpriseInfo映射到HomeLogonHomeViewModel。 I would suggest naming the Msg class in the domain to SysMsg or visa versa. 我建议将域中的Msg类命名为SysMsg,反之亦然。 Then explicity map the CompanyName member and the SysMsg member. 然后显式映射CompanyName成员和SysMsg成员。 Something like this... 像这样

Mapper.CreateMap<EnterpriseInfo,HomeLogonHomeViewModel>()
  .ForMember(dest=>dest.CompanyName,opt=> opt.Name)
  .ForMember(dest=>dest.SysMsg, opt=>opt.SysMsg);

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

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