在我的asp.net MVC 3應用程序中,我嵌套佈局。我按照以下鏈接:asp.net MVC 3剃刀布局錯誤
http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx
我主要佈局頁is_MasterLayout.cshtml再嵌套佈局頁_fullLayout.cshtml。在_fullLayout.cshtml,我有:
@this.RedefineSection("BodyTitle")
@this.RedefineSection("Showcase")
但我越來越這些線。錯誤是:
編譯錯誤
說明:該請求提供服務所需資源的編譯過程中出現錯誤。請查看以下具體的錯誤細節並適當修改您的源代碼。
編譯器錯誤消息:CS1928:「ASP._Page_Views_Shared__fullLayout_cshtml」不包含關於「RedefineSection」和最好的擴展方法過載「SectionExtensions.RedefineSection(System.Web.WebPages.WebPageBase,字符串)」的定義有一些無效參數
源錯誤:
第9行:
第10行:} 第11行:@ this.RedefineSection( 「BodyTitle」) 第12行:@ this.RedefineSection( 「櫥窗」)13 線: @RenderBody()
幫助我的方法是這樣定義的:
public static class SectionExtensions
{
private static readonly object _o = new object();
public static HelperResult RenderSection(this WebPageBase page, string sectionName, Func<object, HelperResult> defaultContent)
{
if (page.IsSectionDefined(sectionName))
return page.RenderSection(sectionName);
else
return defaultContent(_o);
}
public static HelperResult RedefineSection(this WebPageBase page, string sectionName)
{
return RedefineSection(page, sectionName, defaultContent: null);
}
public static HelperResult RedefineSection(this WebPageBase page, string sectionName, Func<object, HelperResult> defaultContent)
{
if (page.IsSectionDefined(sectionName))
page.DefineSection(sectionName,() => page.Write(page.RenderSection(sectionName)));
else if (defaultContent != null)
page.DefineSection(sectionName,() => page.Write(defaultContent(_o)));
return new HelperResult(_ => { });
}
}
請提出解決方案。
問候, 阿西夫·哈米德
我像這樣使用它:@this。RedefineSection(「BodyTitle」,@
默認SubLayout標題
) @ this.RedefineSection(「櫥窗」,@默認SubLayout展示
),但還是同樣的錯誤 – DotnetSparrow 2011-12-17 12:39:13一個這樣說,我並沒有定義內容頁本節。 – DotnetSparrow 2011-12-17 12:43:05
@RenderSection(「TitleSection」,required:false) @RenderBody()在主佈局 – Neha 2011-12-17 12:49:57