MVC:C#:显示 contentPlaceholder 的内容两次

发布于 2024-08-21 17:50:46 字数 448 浏览 4 评论 0原文

我正在编写一个 MVC C# 应用程序。我使用母版页并且有 其他页面的标题和内容放在contentPlaceholders中,分别是 显示在母版页上。

在母版页上,我希望将 TitleContent 写在

和 <title> 中。标签 并在 <body> 中部分(在 <h1> 标签中)。

我不被允许这样做,因为出于某种奇怪的原因你不被允许这样做 在页面上使用相同的 contentPlaceholder 两次。

到目前为止,我一直在使用 (FindControl("TitleContent").Controls[0] as LiteralControl).Text

中标签,在我开始添加动态内容之前它工作得很好 到 TitleContent 占位符(例如 C# 代码)。

如何让该内容显示两次?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

女中豪杰 2024-08-28 17:50:46

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

If the text you want to display is in ViewData or the Model your views are bound to, you maybe able to access it as many times as you want and set it on any element on your view/master page.

烟雨凡馨 2024-08-28 17:50:46

如果您存储必要的字符串,则可以拥有模型视图类。然后使您的视图具有上述类的类型的强类型。最后在标签 make 中

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

希望

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

它有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文