1

I have Component view and model that contains it's own properties from it's own Schema. I can easily get the values of the fields of this Component using @model.Fieldname. I also have a Page model with it's own properties, which includes it's metadata and I am able to get these values in the Page view as well.

How can I get the value of this Page metadata field in my Component view when this Component is used on the Page?

2 Answers 2

2

There is not one prescribed way to do this. An option would be to include a reference to your page model in your component model, and use the page controller to set the page property on each of the component presentations. You can then access the entire page model from your component models:

@Model.Page.MyProperty

If you only want to do this for selected component models, you can define an interface called HasPage, and implement that for the component models that need information from the page. Then, in the PageController, you can iterate over all your component models and only set the page if the model implements this interface. Like this:

       foreach (var entity in page.ComponentPresentations)
        {
            if (entity is HasPage)
            {
                ((HasPage)entity).Page = page;
            }
        }
2
  • This looks promissing, but I was hoping to only set this for the component presentation that needs it and not all component presentations. How do you set this reference to the page from Component view? Using an interface? Commented Feb 13, 2018 at 15:22
  • In the component view you are already too late. You need to set it in the page controller. But you could definitely use an interface on your component models. I will update my answer with a suggested approach.
    – Quirijn
    Commented Feb 13, 2018 at 15:31
0

Alternatively, you can also create your own custom PageMeta ActionAttributes to get the Page Meta values from Page object and Add to current context store with custom extensions parameters, that can be used to access anywhere in your View.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.