Copied to clipboard

Flag this post as spam?

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


  • Darren Wilson 238 posts 646 karma points
    Aug 05, 2016 @ 16:20
    Darren Wilson
    0

    Displaying nodes by a checkbox selection

    Hi Folks,

    I've got a check box list: mon, tues, wed, thurs, fri.

    I need to be able to list the nodes that match a certain check box selection, for example, if mon and wed are selected on the node check boxes that mon is listed on the mon node and wed on the wed node. I've got a wee macro:

    @{
        var selection = CurrentPage.Site().FirstChild("SurveyRepository").Children("Surveys").Where("chooseARegion == \"mon\""); }
     <ul style="padding:0px; margin:0px; list-style:none !important;"> @foreach(var item in selection){
    <li style="padding:0px; margin:0px; list-style:none !important;">
                <a style="width: 100%;" class="survey" target="_blank" href="@item.surveyUrl")">@item.Name</a>
    </li>
    }
    </ul>
    

    This works, but it only displays anything if only the mon checkbox is selected.

    I'm probably missing something within the Where() statement.

    Thanks for your help.

    Darren

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 05, 2016 @ 16:49
    Nicholas Westby
    0

    I'm guessing the drop down is returning the ID of the prevalue rather than the text for the prevalue. If that's the case, something like this may work:

    var dtService = ApplicationContext.Services.DataTypeService;
    var selection = Model.Content.AncestorOrSelf(1)
        .FirstChild(x => "SurveyRepository".InvariantEquals(x.DocumentTypeAlias))
        .Children.Where(x => "Surveys".InvariantEquals(x.DocumentTypeAlias))
        .Where(x => "mon".InvariantEquals(dtService.GetPreValueAsString(x.GetPropertyValue<int>("chooseARegion"))));
    

    Note also that I converted from the dynamic syntax to the normal syntax (or whatever it's called). Gives you better intellisense.

    Also, note that GetPreValueAsString can be very slow, so you'll probably want to add some caching to help optimize that.

    EDIT: Actually, after rereading what you said, it may just be that you aren't checking the CSV for an item within the CSV. So maybe split the value on commas and then check if one of the items is "mon"?

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Aug 05, 2016 @ 16:57
    Kevin Jump
    1

    yes, if it's normal checkbox list. it will be comma seperated. so you need to split it

    so the where (using the same syntax as Nicholas) would be

       .Where(x => x.HasProperty("chooseARegion") &&
       x.GetPropertyValue<string>("chooseARegion","").Split(',').Contains("mon"));
    
  • Darren Wilson 238 posts 646 karma points
    Aug 05, 2016 @ 22:03
    Darren Wilson
    0

    Thanks Kevin, Nicolas.

    I'll give this a try!

    Darren

  • Darren Wilson 238 posts 646 karma points
    Aug 15, 2016 @ 13:55
    Darren Wilson
    0

    Hi Kevin, Nicolas,

    Sorry for the delay getting back to you. Sorry, this is throwing up an error loading. Here's the macro - I'm sure this is something I'm doing.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
    
    var dtService = ApplicationContext.Services.DataTypeService;
    var selection = Model.Content.AncestorOrSelf(1)
    .FirstChild(x => "SurveyRepository".InvariantEquals(x.DocumentTypeAlias))
    .Children.Where(x => "Surveys".InvariantEquals(x.DocumentTypeAlias))
    .Where(x => x.HasProperty("chooseARegion") && x.GetPropertyValue<string>("chooseARegion","").Split(',').Contains("Bellshill"));
    
    }
    
    <ul style="padding:0px; margin:0px; list-style:none !important;">
    @foreach(var item in selection){
        <li style="padding:0px; margin:0px; list-style:none !important;">
            <a style="width: 100%;" class="survey" target="_blank" href="@item.surveyUrl")">@item.Name</a>
        </li>
    }
    </ul>
    

    enter image description here

    This image illustrates what I'm trying to achieve - when the user selects Bellshill from the checkbox, the survey ONLY appears on the Bellshill page. Apologies for my ignorance :-)

    Thanks again for your help.

    Darren

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 15, 2016 @ 15:50
    Nicholas Westby
    0

    What is the error?

  • Darren Wilson 238 posts 646 karma points
    Aug 15, 2016 @ 15:56
    Darren Wilson
    0

    Hi Nicolas,

    Here's the output from the log

    2016-08-15 16:52:44,106 [P2648/D7/T115] WARN umbraco.macro - Error loading Partial View (file: ~/Views/MacroPartials/Surveys.cshtml). Exception: System.Web.HttpCompileException (0x80004005): c:\inetpub\cld\Views\MacroPartials\Surveys.cshtml(18): error CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'surveyUrl' and no extension method 'surveyUrl' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?) at System.Web.Compilation.AssemblyBuilder.Compile() at System.Web.Compilation.BuildProvidersCompiler.PerformBuild() at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) at System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content) at umbraco.macro.LoadPartialViewMacro(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)

    Thanks for getting back to me.

    Darren

  • Darren Wilson 238 posts 646 karma points
    Aug 15, 2016 @ 16:24
    Darren Wilson
    0

    I should check the log more often! [email protected] sorts the error (will need a link added, but one problem at a time ;-)). However, then @item.Name appears on every page not just the bellshill page - so I need @item.Name to display when the datatype checklist name matches the node name. Would changing InvariantEquals(x.DocumentTypeAlias) to InvariantEquals(x.NodeName) have any effect?

    Cheers Darren

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 15, 2016 @ 16:55
    Nicholas Westby
    0

    Why not just have a multinode treepicker to choose the survey (e.g., "Test Survey") from the region page (e.g., "Bellshill")?

    If you can't do that, I would recommend adding the same checkbox list (or perhaps convert it to a drop down) to the region pages and compare against that rather than the node name (content editors like to rename things, and you generally don't want things breaking when nodes are renamed).

  • Darren Wilson 238 posts 646 karma points
    Aug 16, 2016 @ 08:39
    Darren Wilson
    0

    That's what I wanted to do - but this is an older version of Umbraco - tried to update it and landed in a world of pain, with DB and Compile errors. Might just go for the Multi content picker and display the surveys as nodes not list - might not look as nice but will solve a lot of problems.

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 15, 2016 @ 16:27
    Nicholas Westby
    0

    Convert this:

    <a style="width: 100%;" class="survey" target="_blank" href="@item.surveyUrl")">@item.Name</a>
    

    To this:

    <a style="width: 100%;" class="survey" target="_blank" href="@(item.GetPropertyValue<string>("surveyUrl"))")">@item.Name</a>
    

    Since you were using dynamic syntax before and you are no longer using the dynamic data, you'll have to convert to the explicit syntax.

  • Darren Wilson 238 posts 646 karma points
    Aug 16, 2016 @ 11:11
    Darren Wilson
    100

    Fixed this! Just went for the Multi-tree node picker and turned off the list view - this allows the user to pick from a repository of Nodes - this bit of Razor, from our.umbraco, lists the nodes relative to the page the user is on - easy! ha ha - a long way for a short-cut!

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
    
    var bannerList = CurrentPage.surveyPicker.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    var bannerCollection = Umbraco.Content(bannerList);
    foreach (var item in bannerCollection)
    {
        <p>@item.Name</p>
    }
    }
    

    Thanks for all your help on this!

Please Sign in or register to post replies

Write your reply to:

Draft