视图中变量的范围是什么?

时间:2009-08-04 11:43:36

标签: asp.net-mvc variables

如果我在ASP.NET MVC视图中有代码如下:

<%
    bool admin = false;
    if (ViewData.ContainsKey("isAdmin"))
    {
        admin = (bool)ViewData["isAdmin"];
    }
    if (admin)
    {
%>
<%--
    ... generate table of html
--%>

以后在页面中我在另一个代码渲染块<% %>中创建了另一个脚本,我可以重用admin变量并且它会记住页面中较高的状态,还是范围只是在<% %>区块内?

3 个答案:

答案 0 :(得分:7)

以下是关于MVC Scope的注意事项:

内容控件中声明的变量仅限于内容控件中:

    <%@ 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>

**在没有MasterPage的View页面中,在runat =“server”控件中声明的变量仅在该控件中可用。在runat =“server”控件之外声明的变量不可用于该控件。

否则,声明的变量在整个页面都可用。**

<%@ 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>

控制器代码,如果好奇:

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

public ActionResult ScopeTest2()
{

    ViewData["VDScope"] = "ViewDataScope";

    return View();
}

答案 1 :(得分:6)

变量通常在视图中作用域,但它可能会与标记混淆。将整个视图视为单个方法,将标记之外的所有内容(以及标记本身)视为空白。在视图中块外部定义的变量将作用于整个视图。在块内定义的变量(foreach循环/ if then / using)将作用于该块。您不能在先前已在块外定义的块内重用变量。

答案 2 :(得分:3)

试试这个

脚本runat =“server”type =“text / C#”在这里声明了你的变量 string pagename =“你的页面名称”;脚本

然后访问所有ContentPlaceHolderID

相关问题