2016-09-21 25 views
0

我是Umbraco的新手。我有一個名爲視頻的內容中的項目列表。每個項目都有一個特定的類別。我需要從每個類別中檢索「n」個項目。有人請幫忙。此外我正在使用MixItUp jquery插件來顯示項目。需要使用Umbraco在Razor中顯示每個類別的前n項物品

// this will bring up all items from the list 
    var items = Umbraco.TypedContent(Model.Content.Id).Children.Where(x => x.DocumentTypeAlias == "videoItem" && x.IsVisible()); 

// Here am trying to bring 5 items under category "Testimonial" 

    var allItems = items.Where(x => x.GetPropertyValue("category") == "Testimonial").Take(5); 

但我沒有找到任何輸出。請幫忙。

+0

我已經解決了我的問題。它需要鑄造。 var allTestimonial = items.Where(x =>(string)x.GetPropertyValue(「category」)==「Testimonial」)。 –

回答

0

你的代碼第二行應爲:

var allItems = items 
    .Where(x => x.GetPropertyValue<string>("category") == "Testimonial") 
    .Take(5); 

而不是簡單地把結果字符串,這會嘗試將對象轉換爲所需的類型,如果它是不是已經 - see here

如果您使用的是新的ModelsBuilder(非常棒),您還可以選擇強制輸入整個過程。

var items = Model.Content.Children<VideoItem>().Where(x => x.IsVisible()); 

var allItems = items.Where(x => x.Category == "Testimonial").Take(5); 
相關問題