2016-11-04 58 views
0

我創建了一個Web渲染並嘗試通過其路徑獲取特定項目。通過路徑獲取Sitecore MVC中的項目

事情是這樣的:

項項= Sitecore.Context.Database.GetItem( 「/ Sitecore的/內容/家」);

是否有可能使用@ Model.Sitecore()獲取項目?

感謝

+0

你想只得到在MVC渲染一個項目或者你想獲得當前項目/數據源? –

+0

我正在嘗試獲取特定項目。我試圖使用查詢[email protected](/ sitecoe/content/home/global/*)獲取項目,但項目總是空的。 –

+0

好的,我的答案如下。 –

回答

1

我不建議這樣做,但你可以只是@ {}

@{ 
    var item = Sitecore.Context.Database.GetItem("/sitecore/content/home"); 
} 

你真的應該移動到Sitecore的控制器渲染和在做這項工作得到它在你的觀點控制器並返回該項目作爲您的模型。

public class YourController : Controller 
    { 
     public ActionResult Stuff() 
     { 
      var item = Sitecore.Context.Database.GetItem("/sitecore/content/home"); 

      return View(item); 
     } 
    } 

您的看法

@model Sitecore.Data.Items.Item 

<div> 
    @Model.DisplayName 
</div> 
+0

爲什麼你不推薦在WebRendering中使用Sitecord.context.item? –

+0

您不應該在您的視圖中擁有業務邏輯。它糟糕的做法和不可測試的。 –