Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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?
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 ;-)
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.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted