簡體   English   中英

果園ContentItem的AutoroutePart DisplayUrl

[英]Orchard ContentItem's AutoroutePart DisplayUrl

我有一個通過UI(例如,不是通過模塊)構建的自定義內容類型,該類型具有幾個字段,其中一個是ContentItemPicker。 除了設法從Model的項目集合中從ContentItem中找到友好的URL之外,我設法使所有這些東西都與前端一起工作。 我看到一些應使用Url.ImageDisplayUrl([ContentItem])示例,但這給了我這個錯誤: 'System.Web.Mvc.UrlHelper' has no applicable method named 'ItemDisplayUrl' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. 'System.Web.Mvc.UrlHelper' has no applicable method named 'ItemDisplayUrl' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

我在頂部的using語句如下:

@using Orchard.ContentPicker.Fields
@using Orchard.Utility.Extensions;
@using System.Linq
@using Orchard.ContentManagement;
@using Orchard.Mvc.Html;
@using Orchard.Utility.Extensions;

我以為我缺少那些東西,但似乎無法弄清楚是什么。 我建立視圖的方式如下,而我嘗試獲取的URL不在tab.ContentItem.HomepageTab.NavigationItem

/** @Model.Items is a collection of my custom content types that were created through the UI **/
foreach (var tab in @Model.Items)
{
    var t = new SliderTab
    {
        DisplayOrder = tab.ContentItem.HomepageTab.DisplayOrder.Value,
        ButtonText = tab.ContentItem.HomepageTab.ButtonText.Value,
        Description = tab.ContentItem.HomepageTab.Description.Value,
        ImageUrl = tab.ContentItem.HomepageTab.Image.Url,
        Title = tab.ContentItem.TitlePart.Title,
        ContentItem = tab.ContentItem,
        TabText = tab.ContentItem.HomepageTab.TabText.Value
    };

    /** HomepageTab is the custom content type created in the Orchard UI which has a ContentPickerField associated with it. The name on that is NavigationItem, so I just need the friendly URL off of a ContentPickerField's associated ContentItem **/
    if (tab.ContentItem.HomepageTab.NavigationItem != null && tab.ContentItem.HomepageTab.NavigationItem.Ids != null)
    {
        //this is way, super hacky - getting the actual friendly URL would be better
        t.NavigateUrl = "/Contents/Item/Display/" + tab.ContentItem.HomepageTab.NavigationItem.Ids[0];
    }

    tabs.Add(t);
}

** 編輯 **

我在頂部有一個HomepageTab的類聲明,它與tab.ContentItem.HomepageTag不相關,因為它是ContentItem屬性的動態對象。 它的結構如下:

public class HomepageTab
{
    public dynamic DisplayOrder { get; set; }
    public string ImageUrl { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string ButtonText { get; set; }
    public dynamic ContentItem { get; set; }
    public string TabText { get; set; }
    public string NavigateUrl { get; set; }
    public string TabId
    {
        get { return "t" + this.DisplayOrder.ToString(); }
    }
}

思考?

tab.ContentItem.HomepageTab.NavigationItem是您的內容項選擇器字段,但是用這種方式表示,它是一個動態對象,因此,如果您嘗試使用它而不進行轉換,則編譯器可能會感到困惑。 所以首先我建議鑄造:

var pickerField = tab.ContentItem.HomepageTab.NavigationItem as ContentPickerField;
if (pickerField != null) {

然后,您可以在該字段中獲得第一個也是唯一的項目(警告,我們很可能在這里引起選擇的N + 1問題,請參見下文):

    var firstItem = pickerField.ContentItems.FirstOrDefault();

最后,我們可以要求該商品的顯示網址:

    if (firstItem != null) {
        var url = Url.ItemDisplayUrl(firstItem);

這應該很好。 但是請當心:如上所述,獲取每個選項卡的項目集合可能會觸發每個選項卡進行一個新的數據庫查詢,從而降低性能。 為避免該問題,您可以使用類似於我在本文中描述的技術預取相關的內容項: https : //weblogs.asp.net/bleroy/speeding-up-pages-with-lots-of-媒體內容項

除了預先獲取圖像外,您首先要獲取所有相關ID的列表(這是免費的,這些ID存儲在字段中,並與您已有的內容項一起存儲)。 然后,您將使用GetMany為項目構建ID的本地緩存。 最后,您將使用該緩存而不是ContentItems集合從ID中查找項目。

我希望這是有道理的。

暫無
暫無

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

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