简体   繁体   English

如何从asp.net mvc中的PartialView更改PageTitle

[英]how to change PageTitle from PartialView in asp.net mvc

I want to change Page title from partial view. 我想从局部视图更改页面标题。 But following error shown 但是显示以下错误

Using the Title property of Page requires a header control on the page. (eg <head runat="server" />).

My master page head section is here 我的主页头部分在这里

<head runat="server">
    <title>
        <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
    </title>
</head>

My Default page is here 我的默认页面在这里

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    <%=ViewData["pagetitle"] %>
</asp:Content>

ViewData["pagetitle"] returns current page title for example Home, About, News List, News Detail. ViewData["pagetitle"]返回当前页面标题,例如主页,关于,新闻列表,新闻详细信息。 But i want to change current news information title instead of News Detail string. 但是我想更改当前的新闻信息标题,而不是News Detail字符串。 News Detail page contains partial view. News Detail页面包含部分视图。 Partial view know which news shown. 部分视图知道显示了哪个新闻。

Please help 请帮忙

Not really sure how you have your files structured but if you have an entities folder in which you pull information from for your view page then in the aspx page you can do something like this: 不太确定文件的结构如何,但是如果有一个实体文件夹,您可以在其中从视图页面中提取信息,然后在aspx页面中可以执行以下操作:

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    <%:projectName.Resources.Entities.FolderName.FileName%>
</asp:Content>

Because the title is rendered as part of the head section, there is no easy way for a partial view to set the page title directly. 由于标题是作为标题部分的一部分呈现的,因此部分视图无法直接设置页面标题。

But your controller (which fetches the news item to display) will know what the title should be, and can add the "pagetitle" item to the ViewData collection, which you can then set either in your master layout directly or in a page-specific content region as you are doing above. 但是您的控制器(获取要显示的新闻项)将知道标题是什么,并且可以将“ pagetitle”项添加到ViewData集合中,然后可以在主布局中直接设置或在特定于页面的设置中设置上面的内容区域。

Hint: I'd recommend creating a static class called ViewDataKeys with static string properties (or public const fields) used as indexers into the ViewData collection. 提示:我建议创建一个名为ViewDataKeys的静态类,该类具有静态字符串属性(或公共const字段),用作ViewData集合的索引器。 This helps avoid duplicates, spelling mistakes and case-sensitivity errors in working with the string-based dictionary from multiple sources. 在使用多个来源的基于字符串的字典时,这有助于避免重复,拼写错误和区分大小写错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM