Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a the scope of a variables inside a 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 later down the page I create another script inside another code render block <% %>, can I reuse the admin variable and will it remember the state from higher in the page, or is the scope just inside the <% %> blocks?

like image 958
leora Avatar asked Aug 04 '09 11:08

leora


2 Answers

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

<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();
}
like image 154
WWC Avatar answered Oct 27 '22 00:10

WWC


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.

like image 36
tvanfosson Avatar answered Oct 27 '22 00:10

tvanfosson