2010-03-10 53 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

相关问题