简体   繁体   中英

MVC: C#: display content of a contentPlaceholder twice

I'm writing an MVC C# application. I use a masterPage and have the title and content of other pages put in contentPlaceholders, which are displayed on the master page.

On the MasterPage, I want the TitleContent written in both the <title> tag and in the <body> section (in a <h1> tag).

I'm not allowed to do this, because for some odd reason you're not allowed to use the same contentPlaceholder twice on a page.

Until now, I've been using (FindControl("TitleContent").Controls[0] as LiteralControl).Text in the <h1> tag, which worked fine until I started adding dynamic content to the TitleContent placeholder (eg C# code).

How do I display this content twice?

如果要显示的文本位于ViewData或视图所绑定的模型中,您可以根据需要多次访问它,并将其设置在视图/母版页上的任何元素上。

You can have model view class were you store necessary string. Then make you views as strongly typed with the type of the class mentioned above. Finally inside tags make

<title><%=Model.PageName%></title>

and

<h1> <%=Model.PageName%></h1>

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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