开发者

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. (e.g. <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. But i want to change current news information title instead of News Detail string. News Detail page contains partial view. 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:

<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.

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. This helps avoid duplicates, spelling mistakes and case-sensitivity errors in working with the string-based dictionary from multiple sources.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜