Copied to clipboard

Flag this post as spam?

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


  • Gonzalo Matias Borghi 10 posts 92 karma points
    Apr 14, 2016 @ 17:47
    Gonzalo Matias Borghi
    0

    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

    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?

    CSHTML

    @model Encuestas.ViewModels.PollViewModel
    
    @foreach (var currentTheme in Model.Themes.Where("¿¿¿???")
    {
        <div>
            <p>
                <h1 style="text-align: center">@currentTheme.Title</h1>
            </p>
        </div>
    }
    

    Umbraco content

    Polls
        Theme 1
            Question 1
                Answer 1
                Answer 2
                Answer 3
                Answer 4
            Question 2
                Answer 1
                Answer 2
                Answer 3
                Answer 4
            Question 3
                Answer 1
                Answer 2
                Answer 3
                Answer 4
        Theme 2 ==> Active == false
            Question 1
                Answer 1
                Answer 2
                Answer 3
                Answer 4
            Question 2
                Answer 1
                Answer 2
                Answer 3
                Answer 4
            Question 3
                Answer 1
                Answer 2
                Answer 3
                Answer 4
    

    Thx!!!

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Apr 14, 2016 @ 19:14
    Dan Diplo
    0
    Where(x => x.GetPropertyValue<bool>("active")
    

    You may need to add a reference to Umbraco.Web namespace in your controller.

  • Gonzalo Matias Borghi 10 posts 92 karma points
    Apr 14, 2016 @ 19:22
    Gonzalo Matias Borghi
    0

    Thx for you help, but Visual sayme "Cannot resolve symbol 'GetPropertyValue'".

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Apr 14, 2016 @ 20:02
    Nicholas Westby
    100

    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).

  • Gonzalo Matias Borghi 10 posts 92 karma points
    Apr 14, 2016 @ 20:57
    Gonzalo Matias Borghi
    0

    Thx! but, no :( :( :( i try with:

    var themePageList = Umbraco.TypedContent(CurrentPage.Id).Children.Where(x => x.GetPropertyValue<bool>("active"));
    
    var themePageList = CurrentPage.Children.Where(x => x.GetPropertyValue<bool>("active"));
    

    Visual continues: "Cannot resolve symbol 'GetPropertyValue'"

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Apr 15, 2016 @ 00:20
    Nicholas Westby
    0

    You need to add this namespace to get access to that extension method:

    using Umbraco.Web;
    
  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Apr 15, 2016 @ 00:22
    Nicholas Westby
    0

    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:

    Intellisense

  • Gonzalo Matias Borghi 10 posts 92 karma points
    Apr 15, 2016 @ 02:24
    Gonzalo Matias Borghi
    0

    Yes of course. i add Umbraco.web

    enter image description here

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Apr 15, 2016 @ 05:34
    Nicholas Westby
    0

    That's weird. What version of Umbraco do you have? What happens when you type this:

    Global

    What if you change your using statement to this:

    using global::Umbraco.Web;
    
  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Apr 15, 2016 @ 06:15
    Bjarne Fyrstenborg
    0

    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

  • Gonzalo Matias Borghi 10 posts 92 karma points
    Apr 15, 2016 @ 13:12
    Gonzalo Matias Borghi
    0

    ¬¬ ¬¬ ¬¬ Just missing using System.Linq; Resharper or VS nor solved that, it was by chance. Thank you for your support. !!!

Please Sign in or register to post replies

Write your reply to:

Draft