1

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?

3
  • 2
    why are you creating variables in a view? isn't that what the model is for? Commented Aug 11, 2011 at 13:35
  • Excellent point, I should have thought of that. This is the way it has been done in other parts of the application (written by others), and for some reason I just didn't think to do it differently. Commented Aug 11, 2011 at 13:51
  • I'll add an answer so we can closed this question off :) Commented Aug 11, 2011 at 13:52

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.