views:

481

answers:

2

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 later down the page i create another script inside these <% %>, can i reuse the admin variable and will it remember the state from higher in the page of is the scope just inside the <% %> blocks?

+5  A: 

Variables are scoped normally in a view, though it can get confusing with the tags. Think of the whole view as a single method with everything outside the tags (and the tags themselves) as whitespace. A variable defined outside a block in the view will be scoped to the entire view. A variable defined inside a block (foreach loop/if then/using) will be scoped to that block. You can't reuse a variable inside a block that has previously been defined outside the block.

tvanfosson
A: 

Here is what I noticed regarding MVC Scope:

A variable declared in a Content Control is scoped only within the 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>

**In a View Page without a MasterPage, variables declared in a runat="server" control are only available in that control. Variables declared outside the runat="server" control are not available to that 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"&gt;

<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, in case curious:

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

public ActionResult ScopeTest2()
{

    ViewData["VDScope"] = "ViewDataScope";

    return View();
}
WWC