I'm using MVC razor and i loop out news in the order of the dates. I have a list of IDs of featured posts.
var getNews = Model.Content.GetPropertyValue<string>("featuredNews", true).Split(',').ToList();
I want to loop out the featured posts first in the order of the list and then the rest of the News nodes.
What i plan to do is loop out the featured News nodes and then loop out all my nodes after but exclude the node if its ID matches one of the IDs in my featuredNews list.
Is there a better way to do it? Can you build your own node set? This is what i'm working with:
var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);
if (startNode.Children.Where("Visible").Any())
{
foreach (var page in startNode.Children.Where("Visible").OrderBy("CreateDate desc"))
{
...
// Loop out content
...
}
}
Update:
This is what i've got so far and it works. But i'd like the code to be less embarrassing.
@if (Model.MacroParameters["startNodeID"] != null)
{
var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);
var getNews = Model.Content.GetPropertyValue<string>("featuredNews", true).Split(',').ToList();
// Loop out the featured news posts first
if (Model.Content.HasValue("featuredNews", true))
{
foreach (var nodeId in getNews)
{
var page = Umbraco.Content(nodeId);
@RenderNewsPuff(page)
}
}
// Then i loop out the rest of the News Posts but exclude the featured ones i just looped out
if (startNode.Children.Where("Visible").Any())
{
foreach (var page in startNode.Children.Where("Visible"))
{
bool skipNode = false;
foreach(var newsID in getNews){
if(newsID == page.Id.ToString()) {
skipNode = true;
}
}
if(!skipNode) {
@RenderNewsPuff(page)
}
}
}
}
Reorder nodes
I'm using MVC razor and i loop out news in the order of the dates. I have a list of IDs of featured posts.
I want to loop out the featured posts first in the order of the list and then the rest of the News nodes.
What i plan to do is loop out the featured News nodes and then loop out all my nodes after but exclude the node if its ID matches one of the IDs in my featuredNews list.
Is there a better way to do it? Can you build your own node set? This is what i'm working with:
Update:
This is what i've got so far and it works. But i'd like the code to be less embarrassing.
Hi Johan,
How about something like this:
If you want RenderNewsPuff to use the dynamic model you can change the
foreach
toforeach (dynamic node in myCollection)
Jeavon
Edit above as I forgot the order by
Cool! I had to read up on how the lambda expressions worked but it works perfectly :). Isn't it using the dynamic model already?
Brilliant, oh yeah, I think my comment update switched it to the dynamic model!
For strongly typed model it would be:
is working on a reply...