Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Razor 在epserver中呈现不同的部分模板_Razor_Episerver - Fatal编程技术网

Razor 在epserver中呈现不同的部分模板

Razor 在epserver中呈现不同的部分模板,razor,episerver,Razor,Episerver,我有一个页面部分,当页面添加到ContentArea时,它应该呈现在ContentArea内部。这很好,但是现在我在两个不同的页面上有两个不同的ContentArea,我希望在这些页面上添加相同的子页面,以便在每个父页面上呈现不同的内容 我发现我可以在渲染部分时以某种方式使用标记来区分内容区域: @Html.PropertyFor(m => m.MyBlockProperty, new { Tag = RenderingTags.Sidebar }) @Html.PropertyFor(m

我有一个页面部分,当页面添加到ContentArea时,它应该呈现在ContentArea内部。这很好,但是现在我在两个不同的页面上有两个不同的ContentArea,我希望在这些页面上添加相同的子页面,以便在每个父页面上呈现不同的内容

我发现我可以在渲染部分时以某种方式使用标记来区分内容区域:

@Html.PropertyFor(m => m.MyBlockProperty, new { Tag = RenderingTags.Sidebar })
@Html.PropertyFor(m => m.MyContentArea, new { Tag = RenderingTags.Sidebar })

但是,在我的SomePage.cshtml(这是我的部分视图)中,我是否在这里得到了一个变量或其他东西,以便我知道需要哪个标记?或者是否有一些命名约定,比如SidebarSomePage.cshtml,以便我可以定义多个部分模板?我必须创建一个控制器来处理这个问题吗?这对我来说似乎没有必要,我只想根据页面的不同稍微更改html…

我很确定您可以从视图(或控制器)中的ViewData字典访问标记,如下所示:

@ViewData["Tag"]
还可以将任何其他设置传递给视图

@Html.PropertyFor(m => m.MyContentArea, new { Tag = RenderingTags.Sidebar, RenderThisPartialDifferently = true, ShowHeading = false })
然后访问它们:

@ViewData["RenderThisPartialDifferently"]
@ViewData["ShowHeading "]
然后,您可以选择在两者之间放置一个控制器,并渲染一个完全不同的视图


非常确定标记视图也有命名约定。但我确实知道,您可以在/shared/displaytemplates中放置与标记同名的视图。但这不是您现在所要求的。

创建一个
部分内容控制器
,然后使用
模板描述符属性
指定您不想使用的标记。然后使用视图中Johan解释的属性

选择用于呈现内容实例的模板取决于特定上下文,如频道和标记。对于要自动注册的模板,它必须实现EPiServer.Web.IRenderTemplate(其中T表示它可以呈现哪个模型)。如果为模板使用基类,如PageBase、ContentControlBase、BlockControlBase、PageController、PartialContentController或BlockController,则不需要显式实现接口,因为这是由基类完成的。此外,您可以使用TemplateDescriptorAttribute指定有关模板的更多详细信息,如标记和继承,以及稍后有关该主题的更多信息


除所有答案外,您还可以使用模板注册器为特定标记注册其他模板

[ServiceConfiguration(typeof(IViewTemplateModelRegistrator))]
public class TemplateCoordinator : IViewTemplateModelRegistrator
{

    public void Register(TemplateModelCollection viewTemplateModelRegistrator)
    {
        viewTemplateModelRegistrator.Add(typeof(MyBlock), new TemplateModel
        {
            Tags = new[] { RenderingTags.Sidebar },
            AvailableWithoutTag = false,
            Path = BlockPath("Some-Other-Template.cshtml")
        });
    }
}
这将确保如果块被呈现在“内部”
RenderingTags.Sidebar
context(例如通过
Html.PropertyFor(....,new{tag=RenderingTags.Sidebar}))
文件
将使用其他一些模板.cshtml


AlloyTech有示例代码。

要访问标记,我应该在partial SidebarSomePage.cshtml中写些什么?谢谢!这工作做得很好!我使用的@-符号一开始让我有点困惑。我只是使用了与你问题中相同的语法。请更改您的问题以反映您的剃须刀语法:)哦,我没有意识到!