sharptooth
sharptooth

Reputation: 170549

Page title not changed when using @RenderPage

I'm using @RenderPage to reuse the code across two similar pages. Each of the two pages consists of just @RenderPage:

@RenderPage("~/Views/PathToImplementation.cshtml", new { paramsListHere })

the code reused (inside PathToImplementation.cshtml) uses ViewBag.Title to set the page title:

ViewBag.Title = somethingUseful;

The problem is changing ViewBag.Title from inside the reused code has no effect on the original page - nothing happens.

How do I change the title of a page from the code called by @RenderPage?

Upvotes: 2

Views: 472

Answers (2)

sharptooth
sharptooth

Reputation: 170549

Looks like when RenderPage is used the ViewBag is duplicated and the copy is passed into the page which RenderPage renders and there's no way back. So the cleanest way I've found so far is passing a reference to the page that calls RenderPage. Something like this:

//calling page
@RenderPage( PathToPage.cshtml, new { ParentPage = this } )

//PathToPage.cshtml
@{  WebViewPage parentPage = PageData["ParentPage"];
    parentPage.ViewBag.Title = blahblahblhah;      
}

Upvotes: 2

Kaf
Kaf

Reputation: 33839

If your title is inside PathToImplementation.cshtml and using a parameters to set the title within PathToImplementation.cshtml, your parameters should be accessed using PageData. MSDN.

Here is an example

Ex;

   @PageData[index]; //Or 
   @PageData["title"];

Upvotes: 0

Related Questions