Asp.net-mvc – How to set the page title in an ASP.NET MVC view

asp.net-mvcview

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?

Best Solution

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.