Hi! I have a question. I need to list the children with the "Active == true" property from a custom controller and from the .cshtml
Controller
public class PollsController : SurfaceController
{
private List<Theme> ThemeList()
{
var themePageList = Umbraco.Content(CurrentPage.Id).Children.Where("¿¿¿???");
var themeList = new List<Theme>();
foreach (var currentTheme in themePageList)
{
themeList.Add(
new Theme
{
ID = currentTheme.Id,
Title = currentTheme.Name,
}
);
}
return themeList;
}
}
public ActionResult RenderPoll()
{
var viewModel = new PollViewModel
{
Themes = ThemeList()
};
return PartialView("~/Views/Partials/_Polls.cshtml", viewModel);
}
And if to not filter on the controller, as I do to filter the view?
First of all, I would recommend using Umbraco.TypedContent rather than Umbraco.Content. This will ensure you are not using dynamic syntax, which will ensure you have intellisense. Here's an example:
var themePageList = Umbraco.TypedContent(CurrentPage.Id).Children.Where(x => x.GetPropertyValue<bool>("active"));
Actually, you can simplify that further:
var themePageList = CurrentPage.Children.Where(x => x.GetPropertyValue<bool>("active"));
This assumes you have a boolean property on your content nodes called "Active" (with an alias of active).
Filter children with a custom property
Hi! I have a question. I need to list the children with the "Active == true" property from a custom controller and from the .cshtml
Controller
And if to not filter on the controller, as I do to filter the view?
CSHTML
Umbraco content
Thx!!!
You may need to add a reference to
Umbraco.Web
namespace in your controller.Thx for you help, but Visual sayme "Cannot resolve symbol 'GetPropertyValue'".
First of all, I would recommend using
Umbraco.TypedContent
rather thanUmbraco.Content
. This will ensure you are not using dynamic syntax, which will ensure you have intellisense. Here's an example:Actually, you can simplify that further:
This assumes you have a boolean property on your content nodes called "Active" (with an alias of
active
).Thx! but, no :( :( :( i try with:
Visual continues: "Cannot resolve symbol 'GetPropertyValue'"
You need to add this namespace to get access to that extension method:
By the way, you can use intellisense in Visual Studio 2015 to figure out which namespace you need for things that don't appear to exist:
Yes of course. i add Umbraco.web
That's weird. What version of Umbraco do you have? What happens when you type this:
What if you change your using statement to this:
Hi Gonzalo
Does it make any difference if you use the method
.Children()
instead of the property.Children
?Like in this example: http://umbracodevelopers.sdesign1dev.co.uk/2014/9/3/json-surface-controller/
/Bjarne
¬¬ ¬¬ ¬¬ Just missing using System.Linq; Resharper or VS nor solved that, it was by chance. Thank you for your support. !!!
is working on a reply...