2011-12-17 140 views
0

在我的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(_ => { }); 
    } 

} 

请提出解决方案。

问候, 阿西夫·哈米德

回答

0

RedefineSection帮手包含两个或多个参数

RedefineSection(this WebPageBase page, string sectionName) 

,但你只有一个路过在你看来

@this.RedefineSection("BodyTitle") 
@this.RedefineSection("Showcase") 

根据

http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx

你必须把东西

@this.RedefineSection("TitleSection", 
        @<h1>Default SubLayout title</h1>) 
+0

我像这样使用它:@this。RedefineSection(“BodyTitle”,@

默认SubLayout标题

) @ this.RedefineSection(“橱窗”,@

默认SubLayout展示

),但还是同样的错误 – DotnetSparrow 2011-12-17 12:39:13

+0

一个这样说,我并没有定义内容页本节。 – DotnetSparrow 2011-12-17 12:43:05

+0

@RenderSection(“TitleSection”,required:false) @RenderBody()在主布局 – Neha 2011-12-17 12:49:57

0

以下步骤解决您的问题 1.让你的类“SectionExtensions”的命名空间。 2.在您使用RedefineSection的布局页面中,使用

@using yournamespace导入该页面顶部的名称空间。

谢谢。

+0

该类不在任何命名空间下。我用名称空间附上它,并在顶部添加“@using CreditRegistry.Models”,但仍然是相同的错误。 – DotnetSparrow 2011-12-17 12:41:51

+0

将命名空间放在两个materpage中。主布局以及嵌套。 – dotnetstep 2011-12-17 12:58:57