ASP.NET MVC - 视图继承

发布于 2024-11-29 11:15:59 字数 831 浏览 1 评论 0原文

这个问题与这个问题有点相关:如何在 ASP.NET MVC 页面中声明全局变量

我想知道是否可以在主视图(在两个单独的部分中)中声明和使用变量,然后在继承此主视图的某些视图中,在显示之前更改其值。我还没有实现这个,所以我不能只是测试它,但在我实际编码之前了解它是如何工作的会很有帮助,这样我就可以第一次就做对。

我的想法是这样的。

母版页:

<div id="OnePartOfThePage">
Somecontentdisplayedhere
<% string textforlater = "I am a master page"; %>
</div>

<asp:ContentPlaceHolderID="MainContent" runat="server" />


<div id="AnotherPartOfThePage">
<%: textforlater %>
<div>

在继承页面中:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
MoreContentHere
<% textforlater = "I am not the master page, I am it's child"; %>
</asp:Content>

这是一个可行的想法,还是应该在每个子项中单独创建和显示字符串?

This question is a bit related to this one: How to declare a global variable in ASP.NET MVC page.

I'm wondering if it is possible to declare and use a variable in a master view (in two separate sectoins), and then, in certain views inheriting this master view, change its value before it is displayed. I haven't gotten to the implementation of this yet, so I can't just test it, but it would be helpful to know how it works before I actually code it so I can do it right the first time.

What I have in mind is something like this.

Master page:

<div id="OnePartOfThePage">
Somecontentdisplayedhere
<% string textforlater = "I am a master page"; %>
</div>

<asp:ContentPlaceHolderID="MainContent" runat="server" />


<div id="AnotherPartOfThePage">
<%: textforlater %>
<div>

And in the inheriting page:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
MoreContentHere
<% textforlater = "I am not the master page, I am it's child"; %>
</asp:Content>

Is this a workable idea, or should I create and display the string separately in each child?

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

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

发布评论

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

评论(1

荆棘i 2024-12-06 11:15:59

您不应该使用视图来存储变量,这就是模型的用途

You shouldn't be using the view to store variables, that's what the model is for

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