The "position" parameter, is actually the name of a contnt picker.
Inside my Razor Partial View Macro File, I just want to loop through
the nodes for that content picker.
I've already seen how I can get and use each node in the picker:
var position = Model.MacroParameters["position"];
var dynamicPublishedMNTPNodeList = new List<string>();
foreach (var id in CurrentPage.cprLatestNews)
{
dynamicPublishedMNTPNodeList.Add(id.InnerText);
}
var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
var recordCount = dynamicMNTPCollection.Count();
if (dynamicMNTPCollection.Any())
{
foreach (var item in dynamicMNTPCollection)
{
@item.Id
}
}
}
But in this case, I want to pass in the value "cprLatestNews" inside the position variable like this:
foreach (var id in CurrentPage.@position)
But I get:
Cannot implicitly convert type 'object' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
I've also tried:
foreach (var id in CurrentPage.GetProperty(@position))
and
foreach (var id in CurrentPage.GetProperty(@position).ToList())
I started reading this
but it seems like there should be a simpler way.
Here is an example from the build in script List Child Pages From Changeable Source maybe it could help you, to see how it´s done
@inherits Umbraco.Web.Macros.PartialViewMacroPage @* === Macro Parameters To Create === Show:True Alias:nodeId Name:Node ID Type:Content Picker *@
@if (Model.MacroParameters["startNodeID"] != null) { @* Get the start node as a dynamic node *@ var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);
if (startNode.Children.Where("Visible").Any()) { <ul> @foreach (var page in startNode.Children.Where("Visible")) { <li><a href="@page.Url">@page.Name</a></li> } </ul> } }
Thanks, I tried a variation of that earlier, but I still get:
2014-02-08 14:05:33,104 [7] WARN umbraco.macro - [Thread 10] Error loading Partial View
(file: ~/Views/MacroPartials/rCprFeature.cshtml). Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Umbraco.Web.Models.DynamicPublishedContent.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result)
at CallSite.Target(Closure , CallSite , Object , String )
at ASP._Page_Views_MacroPartials_rCprFeature_cshtml.Execute() in c:\inetpub\<removed>\Views\MacroPartials\rCprFeature.cshtml:line 103
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult)
at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode currentPage)
at umbraco.macro.LoadPartialViewMacro(MacroModel macro)
at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId)
Ah, it seems that the GetPropertyValue method is a extension so not available on the CurrentPage dynamic object. So we need to use the strongly typed object instead:
@using Umbraco.Core.Dynamics
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@*
=== Macro Parameters To Create ===
Show:True Alias:nodeId Name:Node ID Type:Content Picker
*@
@{
if (Model.MacroParameters["position"] != null)
{
var position = Model.MacroParameters["position"].ToString();
if (Model.Content.HasValue(position))
{
var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>(position));
var typedPublishedMNTPNodeList = new List<string>();
foreach (dynamic id in typedMultiNodeTreePicker)
{
typedPublishedMNTPNodeList.Add(id.InnerText);
}
var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);
foreach (var item in typedMNTPCollection)
{
<p>sdsd: @item.Name</p>
}
}
}
}
Get Property via Macro Parameter
Umbraco 6.1.6
I have a multi node picker, the name of which I want to pass through macro parameter on a template, like this:
The "position" parameter, is actually the name of a contnt picker. Inside my Razor Partial View Macro File, I just want to loop through the nodes for that content picker.
I've already seen how I can get and use each node in the picker:
But in this case, I want to pass in the value "cprLatestNews" inside the position variable like this:
But I get:
I've also tried:
and
I started reading this but it seems like there should be a simpler way.
Hi Jacob,
Have you tried something like this:
Here is an example from the build in script List Child Pages From Changeable Source maybe it could help you, to see how it´s done
Hope that helps
/Dennis
I tried this:
and no error, but it doesn't return anything. I'll try the other tomorrow. thx
Dennis,
I think this example is not applicable because it is starting with a nodeId (startNodeID)
In my case, CurrentPage.[the name of my picker] returns some XML, not a node id.
The value of my 'position' variable is just a text string.
I even tried CurrentPage.GetProperty("cprLatestNews")
I can see that that and this one: CurrentPage.cprLatestNews returns:
But when I try to use either of those with position, @position, "@position" even @CurrentPage.GetProperty(["@]p... I get nothing every time.
Hi Jacob,
I think you looking for this?
Jeavon
Thanks, I tried a variation of that earlier, but I still get:
Ah, it seems that the GetPropertyValue method is a extension so not available on the CurrentPage dynamic object. So we need to use the strongly typed object instead:
You can switch back to dynamics if you want to for the output by changing the foreach, e.g.
Thanks that worked. Understanding more, the mystery unraveling. Thanks Dennis too.
is working on a reply...