我有一个控制器的动作和查看页面,使用母版页。如何在控制器的操作或查看页面中设置母版页的HTML标题?
母版页有一个像HTML标题部分:
<title>this is the page's title</html>
我如何可以访问此节从我的控制器的动作(最好)或我的行动的看法页面内?
我有一个控制器的动作和查看页面,使用母版页。如何在控制器的操作或查看页面中设置母版页的HTML标题?
母版页有一个像HTML标题部分:
<title>this is the page's title</html>
我如何可以访问此节从我的控制器的动作(最好)或我的行动的看法页面内?
<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);
}
从操作方法弹出你想要的标题为ViewData的,然后就使它对网页...
在操作方法
ViewData["PageTitle"] = "Page title for current action";
在母版页
<!-- MVC 1.0 -->
<title><%=Html.Encode(ViewData["PageTitle"]) %></title>
<!-- MVC 2.0 -->
<title><%: ViewData["PageTitle"] %></title>
是的问题是我为每个动作使用强类型ViewData。 – Blankman 2010-03-10 21:05:22
我通常处理设置html页面标题的方式是通过页面Title属性。
在我看来,我有这个...
<%@ Page Language="C#" Title="Hello World" ... %>
而在我的母版页我有这个...
<title><%=Page.Title%></title>
如果你想拥有控制器设置页面标题您可能不得不通过视图模型发送标题。
但是在动态页面上,你怎么才能在标题中硬编码'hello world'? – Blankman 2010-03-10 21:04:43
duplicate http://stackoverflow.com/questions/326628/asp-net-mvc-view-with-master-page-how-to-set-title – 2010-03-10 21:05:11