2014-10-01 40 views
2

我想知道爲什麼對於我的某個視圖,我得到錯誤」Section not defined:「featured2」「。佈局的路徑是正確的,我定義了featured2在查看部分。cshtml view「Section not undefined:」featured2「

查看

@using System.Data; 
@using System.Data.SqlClient; 
@{ 
ViewBag.Title = "Attempted Unauthorized Access"; 
Layout = "../Shared/Layout2.cshtml"; 
string whoareyoupeople = User.Identity.Name.ToString(); 
DateTime date = DateTime.Now; 
string myerrorstring = "User " + whoareyoupeople + " attempted unauthorized access on " + date + "."; 

    string fileName = "C:\\BillingExport\\SECURITY\\seclog.txt"; 
    using (FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write)){ 
    using (StreamWriter sw = new StreamWriter(fs)) 
    { 
     sw.WriteLine(myerrorstring); 
    } 
} 
} 
@section featured2 { 
<body> 
    <head></head> 
<center><h2 style="color:red">Access Denied for user @User.Identity.Name. You are not authorized to view this application.</h2></center> 
    </body> 
} 

佈局2

@{ 
ViewBag.Title = "Layout2"; 
} 

<link runat="server" rel="icon" href="../Content/images/eifavicon.ico" type="image/x-icon" /> 
@RenderSection("featured2") 
@Scripts.Render("~/bundles/jquery") 
@RenderSection("scripts", required: false) 
       <script> 
             browsername = navigator.appName; 
             if (browsername.indexOf("Microsoft") != -1) { 
              browsername = "MSIE"; 
              document.write("<p id='indent2'>You're viewing this page using Internet Explorer. Please use a supported browser (<a href='https://www.google.com/chrome/browser/'>Chrome</a>, <a href='https://www.mozilla.org/en-US/firefox/new/'>Firefox</a>)</p>"); 
             } 

</script> 
+0

您能夠成功地建立自己的解決方案?嘗試關閉並重新打開視覺工作室,這不應該發生。 – 2014-10-01 14:51:37

+0

是的,它建立。我試過重新啓動Visual Studio 2012,沒有解決問題。當我在調試器運行時嘗試訪問視圖時,它恰好對着我的Layout2.cshtml,並將featured2突出顯示爲未定義。 – Dave 2014-10-01 15:10:40

+0

您的視圖如何呈現?它在RenderPage()聲明中嗎? – 2014-10-01 15:27:46

回答

0

ç鮑爾的問題是一個有效的,你從RenderPage渲染這種觀點()?

您無法在模板中定義由 RenderPage()渲染的部分。部分實際上只能從Controller傳遞的實際View 模板中定義。

Similar Stack overflow question/answer