Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what I noticed regarding MVC Scope:</p> <p><strong>A variable declared in a Content Control is scoped only within the Content Control:</strong></p> <pre><code> &lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt; ScopeTest &lt;% string testVar1 = "This is a test."; %&gt; &lt;%= testVar1 %&gt; &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;h2&gt;ScopeTest&lt;/h2&gt; The below reference to the testVar1 will cause a parser error, because the variable is out of scope. &lt;%=testVar1 %&gt; &lt;/asp:Content&gt; </code></pre> <p>**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. </p> <p>Otherwise, declared variables are available throughout the page.**</p> <pre><code>&lt;%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;% string strPageScope = "PageScope Variable."; %&gt; &lt;head runat="server"&gt; [HEAD]&lt;br /&gt; &lt;title&gt;ScopeTest2&lt;/title&gt; &lt;% string strHeadScope = "Head Scope..."; %&gt; This WORKS: &lt;%= strHeadScope %&gt; &lt;br /&gt; ViewData available everywhere: &lt;%= ViewData["VDScope"].ToString() %&gt; &lt;br /&gt; strPageScope is NOT Available BECAUSE THE RUNAT=SERVER: &lt;% //Response.Write(strPageScope); %&gt; &lt;br /&gt; [END OF HEAD]&lt;br /&gt; &lt;/head&gt; &lt;body&gt; [BODY START]&lt;br /&gt; strHeadScope NOT AVAILABLE, BECAUSE THE HEAD HAS RUNAT="SERVER" IN IT. &lt;% //Response.Write(strHeadScope); %&gt; &lt;br /&gt; ViewData is available everywhere: &lt;% Response.Write( ViewData["VDScope"].ToString()); %&gt; &lt;br /&gt; &lt;div&gt; &lt;% string strBodyVar = "Testing Var Declared in Body."; %&gt; &lt;/div&gt; &lt;% Response.Write(strBodyVar); %&gt; &lt;br /&gt; Page Scope works: &lt;%= strPageScope %&gt; &lt;br /&gt; [END BODY] &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Controller Code, in case curious:</p> <p>public ActionResult ScopeTest() { return View(); }</p> <pre><code>public ActionResult ScopeTest2() { ViewData["VDScope"] = "ViewDataScope"; return View(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload