davids
davids

Reputation: 5587

Utilize model methods and properties in another model

Thank you in advance for your help.

I am using inheritance to leverage properties from a base mode like this: HomeModel : BaseModel.

This is working as expected, but now I want to take it one step further. I want to use it one additional level down so the new model inherits everything from HomeModel.

I created a NavModel like this

public class NavModel : HomeModel {
    public string CloudVersion{get;set;}
    public IMxUser Usr {get;set;}
    public ILocalizer Localizer {get;set;}
    public NavModel(IMxUser mxUser, ILocalizer localizer)
    {
        Localizer = localizer;
        Usr = mxUser;
        CloudVersion = ConfigurationManager.AppSettings["cloudVersion"];
    }
}

but it threw the error

'HomeModel' does not contain a constructor that takes 0 arguments.

Is it a bad idea for me to daisy-chain like this? What am I doing wrong? Is there a better approach?

UPDATE

Prior to adding the empty constructor, this is the standard HomeModel constructor:

public HomeModel(IMxUser mxUser, ILocalizer localizer)
{
    Localizer = localizer;
    Usr = mxUser;
    BomSearchJavascriptMethod = GetSearchJavascriptMethod(MnxConst.SearchOptions.BILL_OF_MATERIAL, EnterBOMSearchKeyStr);
    POSearchJavascriptMethod = GetSearchJavascriptMethod(MnxConst.SearchOptions.PURCHASE_ORDER_NUMBER, EnterBOMSearchKeyStr);
}

It would be nice if I could leverage the standard HomeModel in related models, but I am not sure how to pass those with my current method of reference.

Upvotes: 2

Views: 69

Answers (1)

paulroho
paulroho

Reputation: 1266

You have to initialize the base class as well using the initializer like this:

public HomeModel(IMxUser mxUser, ILocalizer localizer)
  : base(mxUser, localizer)
{
    // ...
}

Upvotes: 3

Related Questions