Making the default site page title in MVC3

My Layout file has the following:

@{
    ViewBag.Title = "Default page title";
}

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
</head>
.....

      

In my view, I have:

@{
    ViewBag.Title = "Home";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

      

However, the page title is displayed as the "Default Page Title".

How can I get the title of a page to display as "Home"? I only need the "default page title" if I have not specified a value for ViewBag.Title

in the view.

+3


source to share


2 answers


try it



<title>@(String.IsNullOrEmpty(ViewBag.Title) ? "Default page title" : ViewBag.Title)</title>

      

+3


source


Change the line:

ViewBag.Title = "Default page title"; 

      



to

ViewBag.Title = ViewBag.Title ?? "Default page title"; 

      

+6


source







All Articles