Guy
Guy

Reputation: 67380

How do I set the page title in an ASP.NET MVC view?

In an ASP.NET MVC view I have the following code:

<% Page.Title = ViewData.Model.MyPageTitle; %>

and when I step through it I see the Page.Title value changing to the title that I want but when you look at the page's title shown in the browser it has not been modified. It appears that this value is overwritten by something later on.

Any ideas how the page title should be set if you want to do it dynamically?

Upvotes: 4

Views: 11430

Answers (2)

Marko
Marko

Reputation: 1924

why don't you make contentplaceholder of head area in master then you can define title in every page that inherits that master...

cheers

Upvotes: -1

Chad Moran
Chad Moran

Reputation: 12854

I would recommend 2 options.

First would to set something up in your master page that did something like this.

<% Page.Title = ViewData["Title"] ?? "Default title"; %>

Or set up a content placeholder in the head/title section that you could implement in your views to set the title with HTML.

Upvotes: 0

Related Questions