What is the scope of the variables inside the view?

If I have code inside an ASP.NET MVC view that looks like this:

<% bool admin = false; if (ViewData.ContainsKey("isAdmin")) { admin = (bool)ViewData["isAdmin"]; } if (admin) { %> <%-- ... generate table of html --%> 

and then down the page I create another script inside another block of code rendering <% %> %% <% %> , can I reuse the admin variable and it will remember the state above on the page, or is it just inside the <% %> blocks?

+6
variables asp.net-mvc
source share
3 answers

Variables are usually displayed in a view, although tags can be confusing. Think of the whole idea as a single method with everything outside the tags (and the tags themselves) in the form of spaces. A variable defined outside the block in the view will be bound to the whole view. A variable defined inside a block (foreach loop / if then / using) will be bound to this block. You cannot reuse a variable inside a block that was previously defined outside the block.

+6
source share

Here is what I noticed in the area of ​​MVC:

A variable declared in Content Control is limited only in Content Control:

  <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> ScopeTest <% string testVar1 = "This is a test."; %> <%= testVar1 %> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>ScopeTest</h2> The below reference to the testVar1 will cause a parser error, because the variable is out of scope. <%=testVar1 %> </asp:Content> 

** On the browse page without MasterPage, the variables declared in the runat = "server" control are available only in this control. Variables declared out of control runat = "server" are not available for this control.

Otherwise, declared variables are available throughout the page. **

 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <% string strPageScope = "PageScope Variable."; %> <head runat="server"> [HEAD]<br /> <title>ScopeTest2</title> <% string strHeadScope = "Head Scope..."; %> This WORKS: <%= strHeadScope %> <br /> ViewData available everywhere: <%= ViewData["VDScope"].ToString() %> <br /> strPageScope is NOT Available BECAUSE THE RUNAT=SERVER: <% //Response.Write(strPageScope); %> <br /> [END OF HEAD]<br /> </head> <body> [BODY START]<br /> strHeadScope NOT AVAILABLE, BECAUSE THE HEAD HAS RUNAT="SERVER" IN IT. <% //Response.Write(strHeadScope); %> <br /> ViewData is available everywhere: <% Response.Write( ViewData["VDScope"].ToString()); %> <br /> <div> <% string strBodyVar = "Testing Var Declared in Body."; %> </div> <% Response.Write(strBodyVar); %> <br /> Page Scope works: <%= strPageScope %> <br /> [END BODY] </body> </html> 

Controller code, if interested:

public ActionResult ScopeTest () {return View (); }

 public ActionResult ScopeTest2() { ViewData["VDScope"] = "ViewDataScope"; return View(); } 
+7
source share

try it

script runat = "server" type = "text / C #" declare your varibale string pagename = "YourPageName" here; script

then access to all ContentPlaceHolderID

+3
source share

All Articles