Razor 为什么Sitecore MVC Model.Rendering.Item用页面项而不是视图呈现数据源项初始化?

Razor 为什么Sitecore MVC Model.Rendering.Item用页面项而不是视图呈现数据源项初始化?,razor,sitecore,sitecore7,sitecore-mvc,sitecore7.5,Razor,Sitecore,Sitecore7,Sitecore Mvc,Sitecore7.5,我在Sitecore 7.5中的MVC视图呈现定义为: Item Name: ViewRenderingA Item Template: View Rendering Path: /path/to/ViewRenderingA.cshtml Data Source: {3AD25922-FE0E-45B3-9F2F-7EF61F678E45} // Item Id of the image in the media library 如您所见,此视图呈现的数据源字段设置为媒体库中图像项的GUID

我在Sitecore 7.5中的MVC视图呈现定义为:

Item Name: ViewRenderingA
Item Template: View Rendering
Path: /path/to/ViewRenderingA.cshtml
Data Source: {3AD25922-FE0E-45B3-9F2F-7EF61F678E45} // Item Id of the image in the media library
如您所见,此视图呈现的数据源字段设置为媒体库中图像项的GUID。基本上,我尝试在内容项布局的特定静态点中渲染媒体库中的图像

我在布局文件中静态地绘制此视图渲染,如下所示:

@Html.Sitecore().Rendering("/path/to/ViewRenderingA")
然后在Razor视图实现ViewRenderingA.cshtml文件中,我执行以下操作:

@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@model RenderingModel

<p>Page Item Name: @Model.PageItem.Name</p> @* <- This correctly shows the content item name. *@
<p>Rendering Name: @Model.Rendering.Item.Name</p> @* <- Why does this also show the content item name instead of the item name of the image in the media library that's linked to the View rendering via data source? *@
@使用Sitecore.Mvc
@使用Sitecore.Mvc.Presentation
@模型渲染模型

页面项目名称:@Model.PageItem.Name

@*如果您提供了数据源,则可以使用Sitecore.Context.Item来访问该项目

Item datasource = Sitecore.Context.Item;
如果您的数据源不打算更改,那么一种解决方法是使用其项ID访问它:

dataSource = Sitecore.Context.Database.GetItem(ItemId);
在您的情况下,项目Id将是媒体库内容的Id
我希望这会有所帮助。

如果您尝试使用
RenderingContext.Current.Rendering.item
访问该项目,该怎么办?当您将数据源静态地作为参数添加到绑定中(如我的博客文章中所述)时,它是否起作用?
RenderingContext.Current.Rendering.Item
还将内容项(我认为它称为“context Item”)作为其他2项。仅当我将数据源作为如下参数添加时,它才起作用:
@Html.Sitecore().Rendering(“/path/to/ViewRenderingA”,new{DataSource=“/path/to/DataSource item”})
。看起来设置视图渲染本身的“数据源”字段没有什么区别。嗯,那么我可能会在上添加一个新问题。如果在视图渲染上设置数据源,那么我相信您可以使用:RenderingContext.Current.rendering.RenderingItem.InnerItem。但不确定,我已经有一段时间没有使用它了。在正常情况下,RenderingContext.Current.Rendering.Item将用于获取当前渲染数据源。