将数据从 Sitecore MVC 视图渲染传递到布局渲染
passing data from Sitecore MVC view rendering to layout rendering
我尝试在 Sitecore MVC 视图渲染中使用 ViewBag 将一些数据传递到其相关的布局渲染,但它没有用。在标准 ASP.NET MVC 中,您可以这样做,但在 Sitecore 中也可以这样做吗?如果没有,那怎么传数据?
编辑
为了澄清我的问题,这是我试图做的:
我有这样的布局渲染:
@using Sitecore.Mvc
@using Sitecore.Mvc.Analytics.Extensions
@using Sitecore.Mvc.Presentation
@model RenderingModel
@{
Layout = null;
}
<!doctype html>
<html>
<head>....</head>
<body>
@if (ViewBag.ShowNewsletterPopup == "display")
{
<!-- render proper html markup to show newsletter popup -->
}
else {
<!-- render regular html markup -->
}
<!-- some html markup here -->
@Html.Sitecore().Placeholder("widget")
<!-- some more html markup -->
然后,在内容占位符中进行视图渲染,其代码如下:
@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@using Sitecore.Data.Items
@model RenderingModel
@{
ViewBag.ShowNewsletterPopup = Model.Rendering.Parameters["ShowNewsletterPopup"];
所以,我想要实现的是根据位于这些块之外的视图渲染的设置在两个 html 标记块之间切换。也许有更好的方法可以做到这一点,但我不确定。我看到的是视图渲染代码是在布局渲染之后编译的,所以代码真的没有被执行。
我可以确认 Sitecore MVC 的当前行为不起作用。在以下场景中,MVC 布局具有:
@using Sitecore.Mvc
@{
Layout = null;
ViewBag.Message = "ViewBagData";
}
<html>
<body>
Message: @Html.Partial("View Rendering") <br>
Message: @Html.Sitecore().ViewRendering("View Rendering")
</body>
</html>
视图渲染:
@ViewBag.Mything
您将看到以下输出:
Message: ViewBagData
Message:
潜在的问题是 ViewRendering 类型启动了一个新的 HtmlHelper。尽管将 ViewData 传递给它,但实际写入 ViewData 的字典支持是延迟的(这是 MVC 的东西)。
我认为 ViewBag 数据的行为与 @Html.Partial()
.
的行为相同
有一个相关的topic around TempData on the Sitecore community user voice。
你可以使用类似的东西(虽然感觉像气味):
Sitecore.Context.Items["Message"]
也许看看 this video,它有一个关于渲染相互依赖的概念。
我尝试在 Sitecore MVC 视图渲染中使用 ViewBag 将一些数据传递到其相关的布局渲染,但它没有用。在标准 ASP.NET MVC 中,您可以这样做,但在 Sitecore 中也可以这样做吗?如果没有,那怎么传数据?
编辑
为了澄清我的问题,这是我试图做的: 我有这样的布局渲染:
@using Sitecore.Mvc
@using Sitecore.Mvc.Analytics.Extensions
@using Sitecore.Mvc.Presentation
@model RenderingModel
@{
Layout = null;
}
<!doctype html>
<html>
<head>....</head>
<body>
@if (ViewBag.ShowNewsletterPopup == "display")
{
<!-- render proper html markup to show newsletter popup -->
}
else {
<!-- render regular html markup -->
}
<!-- some html markup here -->
@Html.Sitecore().Placeholder("widget")
<!-- some more html markup -->
然后,在内容占位符中进行视图渲染,其代码如下:
@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@using Sitecore.Data.Items
@model RenderingModel
@{
ViewBag.ShowNewsletterPopup = Model.Rendering.Parameters["ShowNewsletterPopup"];
所以,我想要实现的是根据位于这些块之外的视图渲染的设置在两个 html 标记块之间切换。也许有更好的方法可以做到这一点,但我不确定。我看到的是视图渲染代码是在布局渲染之后编译的,所以代码真的没有被执行。
我可以确认 Sitecore MVC 的当前行为不起作用。在以下场景中,MVC 布局具有:
@using Sitecore.Mvc
@{
Layout = null;
ViewBag.Message = "ViewBagData";
}
<html>
<body>
Message: @Html.Partial("View Rendering") <br>
Message: @Html.Sitecore().ViewRendering("View Rendering")
</body>
</html>
视图渲染:
@ViewBag.Mything
您将看到以下输出:
Message: ViewBagData
Message:
潜在的问题是 ViewRendering 类型启动了一个新的 HtmlHelper。尽管将 ViewData 传递给它,但实际写入 ViewData 的字典支持是延迟的(这是 MVC 的东西)。
我认为 ViewBag 数据的行为与 @Html.Partial()
.
有一个相关的topic around TempData on the Sitecore community user voice。
你可以使用类似的东西(虽然感觉像气味):
Sitecore.Context.Items["Message"]
也许看看 this video,它有一个关于渲染相互依赖的概念。