If you're looking to render a page partial based on a content reference, the easiest way is to convert that content reference into an IContentData object at which point you can use Html.RenderContentData. Ideally this should be done in the controller of the containing page and passed through in a viewmodel but, for the purposes of showing an example in 1 line, I've converted the reference inline.
<div @Html.EditAttributes(x => x.CurrentPage.TopNewsContentPage)>
@{Html.RenderContentData(ServiceLocator.Current.GetInstance<IContentLoader>().Get<IContentData>(Model.CurrentPage.TopNewsContentPage), false);}
</div>
I don't understand why you have a TopNewsContentPage to start with?
The normal approach would be to have that content in a block and change that PageReference to a ContentReference, then @Html.PropertyFor will work as expected.
@Erik - I think it depends what you're using "TopNewsContentPage" for. If it's purely to be pulled into a slot on the home page and displayed as a partial then I'd entirely agree that a block would be a better choice than a page but, I read it to mean that there are pages of type "TopNewsContentPage" which can be accessed as pages in their own right and one of those is chosen as a highlight on the homepage. In that instance it can make more sense to use a page as it saves your editors having to create an additional block where they've already created a page containing all of the information they need to render it on the home page (or wherever else).
When it comes to rendering, it doesn't really matter whether it's a PageReference or a ContentReference. You'd still need to convert it into an IContentData (or possibly just IContent - I can't remember the specifics) in order to render it using PropertyFor at which point it will resolve the appropriate view and render it regardless of whether it's a page or a block. Using PropertyFor on a ContentReference simply displays the name of the content item as described in the original question.
Hello guys,
i am trying to make the page rendered that is a part of other page via a PageReference property. Can't make it work. So, I have:
- public virtual PageReference TopNewsContentPage { get; set; } - in startPage
- created an instance of a page that will be used to fill the 'TopNewsContentPage' property in startPage.
Here is the controller and the view for that 'subpage' - partial:
@Html.PropertyFor(m => m.CurrentPage.TopNewsContentPage)
In the end I just have one line - name of the that partial page rendered on hopePage. Nothing else. No content of the partial page.
By the way the Index method of the partial page is never being hit.
Any advice how to make that partial page to be displayed within the main page?
thank you