skip to Main Content

I have 2 lists in from the MainVM view model, I would like if by iterating through these 2 lists the values can be extracted and added to the other list from the DescRecordsVM view model.

To give an example of what I want to do is iterate the Attachments list and save the values to Records and then iterate the Emails list and save it to Records as well.

Attachments                         Records
ID = 5                             --> ID=5
documentName = "file.pdf"          --> Description = "file.pdf"
dpcumentType="PDF"                 --> Type="PDF"

Emails                                 Records
ID = 7                               --> ID=7
EmailName = "Progress"               --> Description = "Progress"
dpcumentType="GMAIL"                 --> Type="GMAIL"

I wanted to know if this is possible to do if you can give me how the code can be to do this in a method to return the Records View model or is there another way to do it?

Main view models:

public class MainVM
{
    public List<AttachmentVM> Attachments { get; set; }
    public List<EmailVM> Emails{ get; set; }
}

public class AttachmentVM
{
    public Guid Id { get; set; }
    public string documentName { get; set; }
    public string documentType { get; set; }
}

public class EmailVM
{
    public Guid Id { get; set;  }
    public string EmailName{ get; set; }
    public string EmailType { get; set; }
}

New view model

public class DescRecordsVM
{
    public List<DescriptionVM> Records { get; set; }
}

public class DescriptionVM
{
    public Guid Id { get; set;  }
    public string Description{ get; set; }
    public string Type{ get; set; }
}

The controller receives the ID fills the lists from the database.

public async Task<ActionResult> GetCommunications(long id )
{
    var vm = new ViewModels.MainVM();
    vm.Attachments = GetAttachments(id).ToList();
    vm.Emails= GetAttachments(id).ToList();

    // This is where my idea is to iterate through each list 
    // and fill the `Records` list from `DescRecordsVM` 
    // view model with the values.

    return PartialView("view",Records);
}

2

Answers


  1. You can convert your AttachmentVM and EmailVM using Select method.

    var attachmentRecords = vm.Attachments.Select(a => new DescriptionVM
    {
        Id = a.Id,
        Description = a.documentName,
        Type = a.documentType
    });
    
    var emailRecords = vm.Emails.Select(a => new DescriptionVM
    {
        Id = a.Id,
        Description = a.EmailName,
        Type = a.EmailType
    });
    
    var records = attachmentRecords.Concat(emailRecords);
    
    Login or Signup to reply.
  2. I hope I understood correctly.You want to add all the elements(AttachmentVM,EmailVM) inside this to the record class(DescriptionVM).I did this with AutoMapper

    Nuget package:
    1.AutoMapper 2.AutoMapper.Extensions.Microsoft.DependencyInjection

    Controller:

            private readonly IMapper _mapper;
            public HomeController(IMapper mapper)
            {
                _mapper = mapper;
            }
    

    Action

     var DescriptionVMViewModel = _mapper.Map<List<DescriptionVM>>(vm.Attachments);
     var EmailVMViewModel = _mapper.Map<List<DescriptionVM>>(vm.Emails);
      DescRecordsVM _DescRecordsVM = new DescRecordsVM();
      _DescRecordsVM.Records = new List<DescriptionVM>();
      _DescRecordsVM.Records.AddRange(DescriptionVMViewModel);
    
    

    Program:

    // Add services to the container.
    builder.Services.AddAutoMapper(typeof(Program));
    // Auto Mapper Configurations
    var mapperConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new MappingProfile());
    });
    IMapper mapper = mapperConfig.CreateMapper();
    builder.Services.AddSingleton(mapper);
    

    Mapping

      public class MappingProfile : Profile
        {
            public MappingProfile()
            {
                CreateMap<AttachmentVM, DescriptionVM>()
            .ForMember(dest =>
                dest.Id,
                opt => opt.MapFrom(src => src.Id))
            .ForMember(dest =>
                dest.Description,
                opt => opt.MapFrom(src => src.documentName))
              .ForMember(dest =>
                dest.Type,
                opt => opt.MapFrom(src => src.documentType));
                CreateMap<EmailVM, DescriptionVM>()
          .ForMember(dest =>
              dest.Id,
              opt => opt.MapFrom(src => src.Id))
          .ForMember(dest =>
              dest.Description,
              opt => opt.MapFrom(src => src.EmailName))
            .ForMember(dest =>
              dest.Type,
              opt => opt.MapFrom(src => src.EmailType));
            }
        }
    

    example data:

     List<AttachmentVM> GetAttachments(Guid Id)
            {
                List<AttachmentVM> vMs = new List<AttachmentVM>();
                AttachmentVM attachment = new AttachmentVM();
                attachment.Id = Guid.NewGuid();
                attachment.documentName = "ddd";
                attachment.documentType = "tee";
                vMs.Add(attachment);
                attachment = new AttachmentVM();
                attachment.Id = Guid.NewGuid();
                attachment.documentName = "sdsd";
                attachment.documentType = "sdsd";
                vMs.Add(attachment);
                return vMs;
            }
            List<EmailVM> GetEmails(Guid Id)
            {
                List<EmailVM> vMs = new List<EmailVM>();
                EmailVM attachment = new EmailVM();
                attachment.Id = Guid.NewGuid();
                attachment.EmailName = "tete";
                attachment.EmailType = "s";
                vMs.Add(attachment);
                attachment = new EmailVM();
                attachment.Id = Guid.NewGuid();
                attachment.EmailName = "ssdsdddsd";
                attachment.EmailType = "sds";
                vMs.Add(attachment);
                return vMs;
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search