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(); }
WWC
source share