7


I am trying to get page title from a partial page but i coundn't do it in MVC. Do you have any idea?

in a child.cshtml:

@{ViewBag.Title="this is child"} > this is not working in a child 

I tried to get information ViewData like:

in a viewpage.cshtml

ViewBag.Title = ViewData["pagetitle"]; 

in a child.cshtml:

ViewData["pagetitle"] = "this is child"; 
3
  • 1
    Duplicate of http://stackoverflow.com/questions/11509996/set-a-page-title-from-a-partialview Commented Nov 4, 2015 at 11:34
  • Are you using _Layout.cshtml? If so, how are you displaying the @ViewBag.Title? Commented Nov 4, 2015 at 11:37
  • i am using _layout.cshtml and i don't have a problem with changing page title in a view. My partial is part of view. _Layout > _viewpage > _partialpage and your link is not working for me :) Commented Nov 4, 2015 at 11:43

2 Answers 2

10

Unfortunately you cannot do it within a partial view.

The reason for this is by the time your partial view gets parsed by the view parser, the main layout (which contains the <title></title> tags) has already been written to your applications response stream ready to be flushed to the browser.

If you REALLY need to then I suppose you could parse the current response stream and use regular expression to match and replace the page title, but I wouldn't suggest this as it's pretty inefficient.

As others have said, your better option (albeit not ideal - one reason being the importance a title tag is to search engines) is set the title using Javascript.

This can be achieved like so:

<script type="text/javascript"> document.title = '@ViewBag.Title'; </script> 
Sign up to request clarification or add additional context in comments.

3 Comments

i understand but it should be a way to move data from partial page to view page right?
No, from all of the investigating I've done in the pass it's not possible. The parent view can pass data to a partial view but not the other way around.
@Cagatay You can accomplish this by creating a ViewModel for your layout that gets created with the partial view's ViewModel. The layout's VM could be placed in the ViewBag or ViewData, but because you're building it during the request for the partial, you can set a Title property on it to whatever you want. I've done this by creating a layout VM with a generic T property for the partial's VM type and I build the layout VM in a base controller so the partial's action doesn't have to worry about it. But not from directly in the view.
4

Setting title like @{ ViewBag.Title = "..."; } in a child view will not work if you have title in your layout. Setting title is not a responsibility of PartalView.

Instead you can use javascript, like this:

document.title = "new title"; 

5 Comments

Google would not recognize the title if set with javascript.
aham, but it should be a way to move data from partial page to view page right?
What do you mean by moving data from partial page to view?
Re: @DZL's point. While you might have access to that data on the client, it happens too late to render the title tag in the response which means the title is not in your page source which probably means search engines won't see it.
@DZL you might find this interesting searchengineland.com/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.