Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Mite Tashev 11 posts 81 karma points
    Dec 16, 2017 @ 05:04
    Mite Tashev

    Partial view fails to get all of my posts

    I'm trying to list a few posts from a few categories on my partial view. This is my code in the view:

    var cat1Posts = Model.Posts.Where(x => x.Categories.Contains("Cat1")).Take(5);
    var cat2Posts = Model.Posts.Where(x => x.Categories.Contains("Cat2")).Take(5);
    var cat3Posts = Model.Posts.Where(x => x.Categories.Contains("Cat3")).Take(5);
    var cat4Posts = Model.Posts.Where(x => x.Categories.Contains("Cat4")).Take(5);

    It's giving me problems though because for some reason I get an error that says sequence contains no elements for some of the categories even though there are more than enough posts in it.

    I'm starting to think that it is happening because this is too much data querying in the Razor view and it should be done in a controller instead. Is there a cap to the amount of posts that can be in the Articulate.Models.ListModel ?

    And if I needed to do this in a controller, how would I go about it? How do I make a URL hit my controller instead of the defaults by Articulate and Umbraco?

  • Dirk De Grave 4534 posts 5980 karma points MVP 3x admin c-trib
    Dec 16, 2017 @ 09:23
    Dirk De Grave


    Suspecting x.Categories.Contains() is the culprit, Contains() will throw this exception if Categories is empty string, so why not try following

    Model.Posts.Where(x => !string.IsNullOrEmpty(x.Categories) && x.Categories.Contains("CatX"))

    Oh btw, moving logic to the controller is always a good idea ;-)


  • Mite Tashev 11 posts 81 karma points
    Dec 16, 2017 @ 18:05
    Mite Tashev

    Yeah, I think the best solution is to move the logic to the controller. Do you have an example how to move my C# logic to the controller and serve it to views?

    I'm having problems finding good documentation about this part of Articulate.

    Also, the null check is a good practice but it didn't fix the problem I had. I'm iterating over one of the lists and it still shows that exception even though when I hit a breakpoint just before it goes over the collection it shows that it has 4 records.

    Here is a proof. The collection zabavaPosts contains 4 elements yet it throws an exception.

    enter image description here

Please Sign in or register to post replies

Write your reply to: