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:
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"?
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 :-)
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)
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?
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).
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.
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>
}
}
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:
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
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:
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"?
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
Thanks Kevin, Nicolas.
I'll give this a try!
Darren
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.
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
What is the error?
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
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
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).
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.
Convert this:
To this:
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.
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!
Thanks for all your help on this!
is working on a reply...