2010-03-10 50 views
1

我有一個控制器的動作和查看頁面,使用母版頁。如何在控制器的操作或查看頁面中設置母版頁的HTML標題?

母版頁有一個像HTML標題部分:

<title>this is the page's title</html> 

我如何可以訪問此節從我的控制器的動作(最好)或我的行動的看法頁面內?

+0

duplicate http://stackoverflow.com/questions/326628/asp-net-mvc-view-with-master-page-how-to-set-title – 2010-03-10 21:05:11

回答

2
<title><%= Model.PageTitle %></html> 

public class MasterModel 
{ 
    public string PageTitle { set; get; } 
} 

public class MyViewModel : MasterModel 
{ 
    /* ... */ 
} 

您可以在控制器動作中設置所需的基類PageTitle屬性。

public ActionResult SomeAction() 
{ 
    MyViewModel model = new MyViewModel(); 
    model.PageTitle = "this is the page's title"; 

    return View (model); 
} 
+0

如何讓母版頁使用MasterModel雖然?您在母版頁中引用模型? – Blankman 2010-03-10 21:07:13

+0

好吧,我只是在主人蔘考了基礎模型,謝謝ALLOT! – Blankman 2010-03-10 21:08:31

+0

是的,母版頁將強類型爲基類(MasterModel),而視圖將使用派生模型類。 – 2010-03-10 21:11:07

0

從操作方法彈出你想要的標題爲ViewData的,然後就使它對網頁...

在操作方法

ViewData["PageTitle"] = "Page title for current action"; 

在母版頁

<!-- MVC 1.0 --> 
<title><%=Html.Encode(ViewData["PageTitle"]) %></title> 

<!-- MVC 2.0 --> 
<title><%: ViewData["PageTitle"] %></title> 
+0

是的問題是我爲每個動作使用強類型ViewData。 – Blankman 2010-03-10 21:05:22

1

我通常處理設置html頁面標題的方式是通過頁面Title屬性。

在我看來,我有這個...

<%@ Page Language="C#" Title="Hello World" ... %> 

而在我的母版頁我有這個...

<title><%=Page.Title%></title> 

如果你想擁有控制器設置頁面標題您可能不得不通過視圖模型發送標題。

+1

但是在動態頁面上,你怎麼才能在標題中硬編碼'hello world'? – Blankman 2010-03-10 21:04:43

相關問題