Partial View Macro + Content Picker + Get Page Property
Umbraco version 6.1.6
Hi, I'm converting an XSLT macro to a razor partial view. I am accessing a content picker on a page in my razor partial view. I am able to get a list of Node ids from the content picker. All the node ids are of the same docType. The docType has a property called "teaser". I can't figure out how to get at each node ids teaser property. Note that I can get the individual node ids and page names. But not the custom properties. I've tried several things, but the macro just generates an generic error (my comments at the bottom.) Can anyone offer me any insights?
Here's what I have:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using Umbraco.Core.Dynamics;
@* variables *@
@{
var targetPage = @Model.MacroParameters["targetPage"];
var size = @Model.MacroParameters["size"];
var numberToDisplay = @Model.MacroParameters["numberToDisplay"];
var featureOn = @Model.MacroParameters["featureOn"];
var featureTitle = @Model.MacroParameters["featureTitle"];
var titleColor = @Model.MacroParameters["titleColor"];
}
@{
if(Model.Content.HasValue("cprHighlights"))
{
var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
var typedPublishedMNTPNodeList = new List<string>();
foreach (dynamic id in typedMultiNodeTreePicker)
{
typedPublishedMNTPNodeList.Add(id.InnerText);
}
var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);
var recordCount = typedMNTPCollection.Count();
if(typedMNTPCollection.Any())
{
<div class="homeHighlightsCarouselOutter">
@if(size == "Small")
{
<div class="head headerColorHome with-rss">
<h3>@featureTitle</h3>
</div>
}
else if(size == "Large")
{
<h3 class="homeHighlightsHeader headerColorHome">Highlights</h3>
}
@foreach (var item in typedMNTPCollection)
{
@* The following works *@
<p>@item.Name</p>
<p>@item.Id</p>
@* But when I try to do this: *@
<p>@item.teaser</p>
@* I just get an error *@
@* I have tried using the Id in item.Id to build a node *@
var x = @Umbraco.Content(@item.Id);
@* and no error, but when I try to access it like this: *@
<p>@x.teaser</p>
@* I just get an error *@
}
</div> <!-- end class="homeHighlightsCarouselOutter -->
}
}
}
Your issue is the additional @ within the code, blocks, e.g var x = @Umbraco.Content(@item.Id); should be var x = Umbraco.Content(item.Id); and var targetPage = @Model.MacroParameters["targetPage"]; should be var targetPage = Model.MacroParameters["targetPage"];
I have just take a look at your code, and there are some things I think that can gives you the error. For the first I have removed the @ sign from where you creates the variables for the macro parameters. The next thing I think can give you the error is when you are written you comment to end the div.
I think class and end are dedicatedwords in some programming lanagues maybe this can give some issues, and then I discovered that you have forgot to close the class in your comment. <!-- end class="homeHighlightsCarouselOutter -->
So Jacob, could you try this one, hopefully it goes better.
@* variables *@ @{ var targetPage = Model.MacroParameters["targetPage"]; var size = Model.MacroParameters["size"]; var numberToDisplay = Model.MacroParameters["numberToDisplay"]; var featureOn = Model.MacroParameters["featureOn"]; var featureTitle = Model.MacroParameters["featureTitle"]; var titleColor = Model.MacroParameters["titleColor"];
}
@{ if(Model.Content.HasValue("cprHighlights")) { var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
var typedPublishedMNTPNodeList = new List<string>(); foreach (dynamic id in typedMultiNodeTreePicker) { typedPublishedMNTPNodeList.Add(id.InnerText); } var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);
Dennis, I tried what you have posted exactly, and when I take out @item.Teaser, it works.
If I have @item.Teaser or @item.teaser, it breaks. All I get on the front end is:
if (Model.Content.HasValue("cprHighlights"))
{
var dynamicPublishedMNTPNodeList = new List<string>();
foreach(var id in CurrentPage.cprHighlights)
{
dynamicPublishedMNTPNodeList.Add(id.InnerText);
}
}
I was thinking that in my earlier example, this is right for getting the node ids out of my MNTP:
var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
But when I try to use it like this:
@{
if(Model.Content.HasValue("cprHighlights"))
{
var dynamicPublishedMNTPNodeList = new List<string>();
var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
foreach (dynamic id in typedMultiNodeTreePicker)
{
dynamicPublishedMNTPNodeList.Add(id.InnerText);
}
var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
foreach (var item in dynamicMNTPCollection)
{
<p>@item.Name</p>
<p>@item.Id</p>
}
}
}
I get error.
Is there a different method for typed vs. dynamic collection?
After foreach loop I used to have this:
var typedMNTPCollection = Umbraco.TypedContent(typedPublishedMNTPNodeList).Where(x => x != null);
But we replaced with this:
var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
So, if I put it back together with DynamicXml method, and strip out some of the extra
stuff for simplicity here, I got this and it works:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using Umbraco.Core.Dynamics;
@* variables *@
@{
var targetPage = Model.MacroParameters["targetPage"];
var size = Model.MacroParameters["size"];
var numberToDisplay = Model.MacroParameters["numberToDisplay"];
var featureOn = Model.MacroParameters["featureOn"];
var featureTitle = Model.MacroParameters["featureTitle"];
var titleColor = Model.MacroParameters["titleColor"];
}
@{
if(Model.Content.HasValue("cprHighlights"))
{
var dynamicPublishedMNTPNodeList = new List<string>();
var typedMultiNodeTreePicker = new DynamicXml(Model.Content.GetPropertyValue<string>("cprHighlights"));
foreach (dynamic id in typedMultiNodeTreePicker)
{
dynamicPublishedMNTPNodeList.Add(id.InnerText);
}
var dynamicMNTPCollection = Umbraco.Content(dynamicPublishedMNTPNodeList);
foreach (var item in dynamicMNTPCollection)
{
<p>@item.Name</p>
<p>@item.Id</p>
<p>@item.Teaser</p>
}
}
}
Is it organizationationally sound? Or am I mixing Typed and Dynamic syntax again? I think I am
Do you have a green tick under the name of a author on each comment? That is the mark solution button so should be clicked only on the correct solution.
Partial View Macro + Content Picker + Get Page Property
Umbraco version 6.1.6
Hi, I'm converting an XSLT macro to a razor partial view. I am accessing a content picker on a page in my razor partial view. I am able to get a list of Node ids from the content picker. All the node ids are of the same docType. The docType has a property called "teaser". I can't figure out how to get at each node ids teaser property. Note that I can get the individual node ids and page names. But not the custom properties. I've tried several things, but the macro just generates an generic error (my comments at the bottom.) Can anyone offer me any insights?
Here's what I have:
Does @item.Teaser work in your loop?
No, and I did try both @item.Teaser and @item.teaser
Your issue is the additional @ within the code, blocks, e.g
var x = @Umbraco.Content(@item.Id);
should bevar x = Umbraco.Content(item.Id);
andvar targetPage = @Model.MacroParameters["targetPage"];
should bevar targetPage = Model.MacroParameters["targetPage"];
Jeavon
Hi Jacob,
I have just take a look at your code, and there are some things I think that can gives you the error. For the first I have removed the @ sign from where you creates the variables for the macro parameters. The next thing I think can give you the error is when you are written you comment to end the div.
I think class and end are dedicated words in some programming lanagues maybe this can give some issues, and then I discovered that you have forgot to close the class in your comment. <!-- end class="homeHighlightsCarouselOutter -->
So Jacob, could you try this one, hopefully it goes better.
Alternatively you could try to remove the end div comment, to check if you get any errors then I have done that in my post.
Hope this can help you closer to get it working.
/Dennis
Thx, Dennis and Jeavon.
Dennis, I tried what you have posted exactly, and when I take out @item.Teaser, it works. If I have @item.Teaser or @item.teaser, it breaks. All I get on the front end is:
It seems I get an error if I try to access any custom property from the docType.
Jeavon, Dennis' post follows your recommendation, except you have showed me this:
Maybe I can get at the teaser property that way? Or is there some way I can see what it inside the item object? Or my var x object?
Hi Jacob,
You are using typed syntax not dynamic, so you need to do
@item.GetPropertyValue("Teaser")
not@item.Teaser
(this would be dynamics)If you are only using the online Umbraco editor it maybe easier to use Dynamics, Typed is mainly used for intellisense in Visual Studio or WebMatrix.
This would be your snippet in dynamic syntax (here
@item.Teaser
should would):Jeavon
Thank you for pointing out dynamic vs. typed syntax and their usage.
The example you give generates error, but I think it's just because of how my nodes in content picker are stored?
If I reduce snippet just down to this:
I can see my node values returned like this:
But when I loop through it as here:
I get
Ah ok, can you confirm your MNTP settings, I'm guessing you are storing as CSV rather than XML?
Try this instead:
My MNTP data type has "XML" checked, and not "CSV".
This one is for "CSV" type, yeah?:
Yes, that's right.
I've just fully tested the below snippet with XML storage and it's working, could you try?
I was thinking that in my earlier example, this is right for getting the node ids out of my MNTP:
But when I try to use it like this:
I get error.
Is there a different method for typed vs. dynamic collection?
After foreach loop I used to have this:
But we replaced with this:
So, if I put it back together with DynamicXml method, and strip out some of the extra stuff for simplicity here, I got this and it works:
Is it organizationationally sound? Or am I mixing Typed and Dynamic syntax again? I think I am
Regarding your last post Jeavon; I tried it and yeah, it works. Yours also doesn't mix the dynamic syntax I think. I thought we tried that!?
That's great! We did try it except for the HasValue bit, but that shouldn't cause an error.....? That final snippet is all dynamic syntax.
You can mix and match dynamic and typed just as long as you understand what you are doing where.
Ok, I diff'd. I see it.
vs.
.....
Thanks for taking the time and explanations, I'm moving fwd again. You are the man.
You're welcome, if you change back to
if (Model.Content.HasValue("cprHighlights"))
does it break?p.s. Please mark solution on correct snippet :-)
No it does not break.
I see in the earlier post semi-colon missing on this one:
Must've been it.
I don't see any option to mark solution. So I just high-fived the correct snippet.
Ah phew, all good then.
Do you have a green tick under the name of a author on each comment? That is the mark solution button so should be clicked only on the correct solution.
I see it and checked it. Thx.
Awesome!
is working on a reply...