By default the new project template for ASP.NET MVC 3 adds the following to the default layout (masterpage in razor):
@ViewBag.Title
<
I like to create a PageTitle ActionFilter attributes rather than editing individual ViewBags
Usage: keep the view the same
@ViewBag.Title
For controller-wide page title:
[PageTitle("Manage Users")]
public class UsersController : Controller {
//actions here
}
For individual views:
public class UsersController : Controller {
[PageTitle("Edit Users")]
public ActionResult Edit(int id) {
//method here
}
}
Attribute Code:
public class PageTitleAttribute : ActionFilterAttribute
{
private readonly string _pageTitle;
public PageTitleAttribute(string pageTitle)
{
_pageTitle = pageTitle;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var result = filterContext.Result as ViewResult;
if (result != null)
{
result.ViewBag.Title = _pageTitle;
}
}
}
Easy to manage and works like a charm.