ASP.NET MVC - 视图继承
这个问题与这个问题有点相关:如何在 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该使用视图来存储变量,这就是模型的用途
You shouldn't be using the view to store variables, that's what the model is for