1

I have a model for my table OWNER

namespace MyApp.Models
{
    public partial class Owner
    {
        public int owner_id { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public string address { get; set; }
        public string city { get; set; }

        public virtual User User { get; set; }
    }
}

and then i have my ViewModel

public partial class OwnerData
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string address { get; set; }
}

My Controller

    public ActionResult Index()
    {
        //Create the object Owner based on the User Identity
        Owner owner = CustomDbFunctions.GetUserEntity(User, db).Owner;

//New instance of the ViewModel
        OwnerData ownerData = new OwnerData();

        ownerData.lastname = owner.lastname;
        ownerData.firstname = owner.firstname;
        ownerData.address = owner.address;

            return View(ownerData);

    }

There's a simpler way in order to convert my owner into ownerData without rewrite evry single property of my object?

Sefira
  • 81
  • 2
  • 7
  • there is a way but it's not simple for sure.keep this way – Selman Genç Jan 09 '14 at 16:51
  • There are tools that use "Reflection" and do this for you by matching properties names. I would keep this way for now, but would add a constructor in the OwnerData that accepts Owner object and does this. – A Khudairy Jan 09 '14 at 17:35
  • 1
    @A Khudairy I think i found a good solution using **Automapper** by doing **Mapper.CreateMap();** and then **OwnerData ownerData = Mapper.Map(CustomDbFunctions.GetUserEntity(User, db).Owner);** – Sefira Jan 09 '14 at 17:43

2 Answers2

2

I think i solved the problem using Automapper

public ActionResult Index()
{
    //Create the object Owner based on the User Identity
    Owner owner = CustomDbFunctions.GetUserEntity(User, db).Owner;

    Mapper.CreateMap<Owner, OwnerData>();
    OwnerData ownerData = Mapper.Map<Owner, OwnerData>(owner);

  return View(ownerData);
}

Or if you prefer a dynamic function

    static TDestination GenerateMappingTable<TSource, TDestination>(TSource inputModel)
    {
        Mapper.CreateMap<TSource, TDestination>();
        return Mapper.Map<TSource, TDestination>(inputModel);
    }

To install Automapper just execute

PM> Install-Package AutoMapper

from the package manager console.

Sefira
  • 81
  • 2
  • 7
  • 1
    Mapper.CreateMap call should once per app domain. We should call it in Application_Start() method. Otherwise it will be called whenever Index() is called – Abdul Rauf Jul 14 '17 at 13:15
1

You can use object initizalizers.That makes it more simple,but I'm not sure this is what you want

OwnerData ownerData = new OwnerData  { 
                          lastname = owner.lastname;
                          firstname = owner.firstname;
                          address = owner.address;
                          };
Selman Genç
  • 100,147
  • 13
  • 119
  • 184