asp.net-mvc-4umbraco7mixitup

Need to display first n items from each category in Razor using Umbraco


I am new to Umbraco. I have a list of items in a content named Videos. Each item have a specific category. I need to retrieve 'n' number of items from each category. Some one please help. Also am using MixItUp jquery plugin to display the items.

// 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);

But I didn't found any output. Please help.


Solution

  • Your second line of code should read:

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

    Rather than simply cast the result to string, this will try and convert the object to the desired type if it isn't already - see here.

    If you're using the new ModelsBuilder (which is awesome) you also have the option of strongly typing the whole process.

    var items = Model.Content.Children<VideoItem>().Where(x => x.IsVisible());
    
    var allItems = items.Where(x => x.Category == "Testimonial").Take(5);