When mapping class to viewModel, some data is lost

3

I have a query that returns a client and its phone, but when mapping the class to the viewModel , I lose the phone data:

query:

public Cliente ObterClientePorId(Guid ClienteId)
{
      var consulta =
            Contexto.Cliente.Join(Contexto.Telefone,
                                  p => p.ClienteId, 
                                  pt => pt.ClienteId,
                                 (p, pt) => new {p, pt}).FirstOrDefault();

  if (consulta != null)
  {
      var cliente = consulta.p;
      cliente.telefone = consulta.pt;

      return cliente;
 }

But when mapping the client class to the viewModel cliente , it loses the data that was in telefone .

Mapping:

    public ClienteViewModel ObterClientePorId(Guid ClienteId)
    {
        //aqui tem os dados de telefone
        var consulta = _cliente.ObterClientePorId(ClienteId);
        //aqui o telefone fica null
        var cliente = Mapper.Map<Cliente, ClienteViewModel>(consulta);

        return cliente;
    }

ClienteViewModel :

public class ClienteViewModel
{
    public ClienteViewModel()
    {
        ClienteId = Guid.NewGuid();
    }

    // campos...

    public List<TelefoneViewModel> TelefoneViewModel { get; set; }

    //mais campos...
}
    
asked by anonymous 27.08.2015 / 20:18

1 answer

4

Tried to map the Phone class to "PhoneViewModel"? I mention this because its ClientViewModel class has a List < PhoneViewModel > and not List < Phone & gt ;.

Maybe this will work:

var telefones = Mapper.Map<List<Telefone>, List<TelefoneViewModel>>(consulta.telefone);
var cliente = Mapper.Map<Cliente, ClienteViewModel>(consulta);
cliente.TelefoneViewModel = telefones;

If it does not work, share your Customer and Phone classes, please.

Hugs.

    
27.08.2015 / 20:49