Try our conversational search powered by Generative AI!

Render a page via PageReference

H F
H F
Vote:
 

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 { getset; }  - 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:

public class AirSimplePageController : PartialContentController<AirSimplePage>
   {
       public AirSimplePageController(IContentLoader contentLoader)
       {
 
       }
 
       [ContentOutputCache(CacheProfile = "AirContentOutputCache")]
       public override ActionResult Index(AirSimplePage currentPage)
       {
           return PartialView(currentPage);
       }
   }

View:
<div class="">
    @Html.PropertyFor(x => x.MainBody, new { CssClass = "no-gutter" })
</div>

- and the call to render 'subpage' from within the view of homePage:
@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

#203613
Edited, Apr 29, 2019 19:09
Vote:
 

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>
#203614
Apr 29, 2019 19:26
H F
Vote:
 

thank you Paul, let me try it.

#203620
Apr 29, 2019 21:05
Vote:
 

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.

#203635
Apr 30, 2019 15:13
Vote:
 

@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.

#203648
Apr 30, 2019 21:42
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.