yoozer8
yoozer8

Reputation: 7489

ASP.NET MVC - View inheritance

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?

Upvotes: 1

Views: 545

Answers (1)

Antony Scott
Antony Scott

Reputation: 21978

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

Upvotes: 1

Related Questions