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