简体   繁体   中英

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:

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. Can someone please help me...

Also, I may ask if this does indeed needs an 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.

You need to map EnterpriseInfo to HomeLogonHomeViewModel. I would suggest naming the Msg class in the domain to SysMsg or visa versa. Then explicity map the CompanyName member and the SysMsg member. Something like this...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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