【问题标题】:ASP.NET MVC - View inheritanceASP.NET MVC - 查看继承
【发布时间】:2011-08-11 13:31:19
【问题描述】:

这个问题和这个有点关系:How to declare a global variable in ASP.NET MVC page

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

我的想法是这样的。

母版页:

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

这是一个可行的想法,还是我应该在每个孩子中分别创建和显示字符串?

【问题讨论】:

  • 为什么要在视图中创建变量?这不就是模型的用途吗?
  • 好点,我应该想到的。这是在应用程序的其他部分(由其他人编写)中完成的方式,出于某种原因,我只是没想过要采取不同的做法。
  • 我会添加一个答案,这样我们就可以关闭这个问题了:)

标签: asp.net asp.net-mvc-2 view master-pages


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多