Kentico 13 Core - Calling in Field From Parent View Model to Childview Model

KSA&D Development asked on September 12, 2021 15:04

I am trying to call in a field from a parent view model into a child view model. To get the hero image from the parent page to the child page.

By itself, this method of calling in the hero image from the parent page to the child page works. However, I am running into issues because of using the Pages field type. We are using the Pages field to load related pages on the view. Again this method of calling in pages this way works.

The issue is because I am calling in the parent hero on the child view model, the pages field is erroring our because I don't have a reference to the hero image.

model.RelatedResources = resourceItem.Fields._RelatedResources.Select(p => ResourceItemViewModel.GetViewModel((ResourceItem)p, pageUrlRetriever, attachmentUrlRetriever));

Error CS7036 There is no argument given that corresponds to the required formal parameter 'parentHero' of 'ResourceItemViewModel.GetViewModel(ResourceItem, IPageUrlRetriever, IPageAttachmentUrlRetriever, ResourcesExpand)'

So my questions are:

Is there a better way to call in a field on a parent view model to a child view model? I this is a good way to do this, how can I null out parentHero on the pages field so it's not trying to look for it everytime I use this:

model.RelatedResources = resourceItem.Fields._RelatedResources.Select(p => ResourceItemViewModel.GetViewModel((ResourceItem)p, pageUrlRetriever, attachmentUrlRetriever));

I am using this method to call in related resources on several different view models: model.RelatedResources = resourceItem.Fields._RelatedResources.Select(p => ResourceItemViewModel.GetViewModel((ResourceItem)p, pageUrlRetriever, attachmentUrlRetriever));

Thanks for any help.

See code below in the answer

Recent Answers


KSA&D Development answered on September 12, 2021 15:18

Here is the code: I couldn't put it all in the same message for some reason:

Child View model:

public ResourcesExpandViewModel ResourcesExpand { get; set; } public ResourcesExpandViewModel parentHero { get; set; }

public static ResourceItemViewModel GetViewModel(ResourceItem resourceItem, IPageUrlRetriever pageUrlRetriever, IPageAttachmentUrlRetriever attachmentUrlRetriever, ResourcesExpand parentHero) { ResourceItemViewModel model = new ResourceItemViewModel();

    //THIS IS WHERE I AM REFERENCING THE FIELD FROM THE PARENT VIEW MODEL
    model.HeroImg = parentHero.Fields.HeroImg;


    model.RelatedResources = resourceItem.Fields._RelatedResources.Select(p => ResourceItemViewModel.GetViewModel((ResourceItem)p, pageUrlRetriever, attachmentUrlRetriever));

    return model;
}

Child Controller: public async Task

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.