0

I have a base class Ad.

public class Ad
{
    public int Id{get;set;}

    public string Title{get;set;}

    public string Price{get;set;}
}

Then I have a lot of derived classes like:

public class CarAd : Ad
{
    public string ModelName {get;set;}
}

public class MobileAd : Ad
{
    public string CompanyName {get;set;}
}

And accordingly I have ViewModels for these classes as well.

public class AdViewModel
{
    public int Id{get;set;}

    public string Title{get;set;}

    public string Price{get;set;}
}

public class CarAdViewModel : AdViewModel
{
    public string ModelName {get;set;}
}

public class MobileAdViewModel : AdViewModel
{
    public string CompanyName {get;set;}
}

I am trying to map derived class with base class using value injecter. Like:

    [HttpPost]
    public async Task<IHttpActionResult> Post([FromBody] CarAdViewModel carAdModel)
    {
        var ad = new Ad();
        ad.InjectFrom(carAdModel);
    }

But it is not mapping properties of derived class. Can somebody tell me if it is possible?

Usman Khalid
  • 3,032
  • 9
  • 41
  • 66
  • carAdViewModel has ModelName property, and Ad doesn't has it, what did you expect it to do ? maybe you need to use CarAd instead of Ad – Omu Aug 31 '15 at 11:24
  • But carAd is inherited by Ad class and I am passing derived class into InjectFrom. So can't it convert Ad into CarAd like polymorphism? – Usman Khalid Aug 31 '15 at 11:30
  • Ad is inherited by carAd and you're passing the base class Ad – Omu Aug 31 '15 at 13:03

0 Answers0