2012-03-13 60 views
3

默認站點頁面的標題我有我的佈局文件中的以下內容:創建在MVC3

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

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

在我看來,我有:

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

但是,頁面標題以「默認頁面標題」出現。

我怎樣才能讓頁面標題顯示爲「Home」?如果我沒有在視圖中指定ViewBag.Title的值,我只想要顯示「默認頁面標題」。

回答

3

試試這個

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

乾杯,我是MVC的新手,並且認爲它很像'Page.Title'的網頁形式。再次感謝 – Curt 2012-03-13 22:50:21

6

更改行:

ViewBag.Title = "Default page title"; 

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

我喜歡這更好的了'ViewBag'依然看好中央資料片頁面標題。 – 2013-03-07 17:33:41