簡體   English   中英

Sitecore 7 MVC使用項目的呈現詳細信息從控制器創建部分渲染

[英]Sitecore 7 MVC create partial rendering from controller using presentation details of the item

使用MVC創建控制器時,可以使用項目上定義的渲染(或其模板標准值)返回PartialView結果,而不必為該項目設置布局和渲染,Sitecore 7中是否有某種方法?

我可以看到如何使用自定義模板字段來執行此操作,但這似乎是一種不明智的操作方式。 有沒有更好的方法來實現以下目的:

public PartialViewResult MyAction(string someParameter)
{
    Item selectedItem;
    //some code here to retrieve sitecore item based on the value of someParameter

    var cshtmlFilePath = selectedItem["MyCustomField"];

    return PartialView(cshtmlFilePath, selectedItem);
}

僅供參考,我的cshtml可能非常簡單,例如:

@model Sitecore.Data.Items.Item
<div>
    <h3>
        @Html.Sitecore().Field("Title", Model)
    </h3>
    <div>
        @Html.Sitecore().Field("Content", Model)
    </div>
</div>

盡管上面的方法行得通,但我不喜歡它,因為沒有合適的接口將渲染分配給項目模板的標准值。 我可以輕松地定義自己的演示模板,該模板具有一個“ Path”字段,然后在“ sitecore / layout / Renderings”下的某個位置創建項目,然后在內容項目的鏈接字段中引用該項目,但似乎應該開箱即用。

我不確定我是否完全理解這里的用意,因此這可能只是部分答案,但是看到您要基於項目本身包含的布局信息來渲染項目時,項目渲染可以提供幫助嗎?

這是信息: http : //www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2012/06/MVC-Item-Renderings-in-the-Sitecore-ASPNET-CMS。 aspx

我意識到返回部分視圖會比“項目渲染”路線靈活一些,但是在正常情況下(即向頁面添加標注)似乎可以使用此方法。

一種解決方案是簡單地為項目分配一個布局(引用cshtml文件),然后使用item.Visualization獲取分配給該項目的布局的文件路徑,並在對PartialView的調用中使用該布局的cshtml文件。

public PartialViewResult MyAction(string contentItem)
{
    Item selectedItem;
    //retrieve sitecore item
    string layoutPath = selectedItem.Visualization.Layout.FilePath;
    return PartialView(layoutPath, modalContentItem);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM