I just started off with Razor and i am running into all kinds of trouble and i was hoping you could help me along my way. I always work with XSLT but for this project we decided to use Razor because of Vorto (specifically made for Multilingual sites).
I'm trying to do something really simple but i can't find any documentation on how to do it. I want to make a list with childpages from a specific page. How would i go around doing this by using id instead of the current selection i'm using. I also want to use the same selection to add the value of a field inside that specific node as h2.
Thank you for your reply. Could you show me how to put this all together, because the problem i keep having when constructing Razor is that i somehow do something wrong which makes my macro fail.
i hardly use MacroPartials anymore. But i can try giving it a go. I presume you are using a macroparameter to set the id of the parent node.
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
var id = 0;
// get id from ex. contentPicker datatype on macro
int.TryParse(Model.MacroParameters["parentNode"].ToString(), out id);
// get IPublishContent object from id
IPublishedContent parent = UmbracoContext.Current.ContentCache.GetById(id);
// get children by documenttype "Module"
IPublishedContent[] children = parent.Children.Where(x => x.DocumentTypeAlias == "Module").ToArray();
}
<h2>@(parent.GetPropertyValue<string>("headline"))</h2>
@if (children.Any()){
<ul>
@foreach (IPublishedContent item in children)
{
<li><a href="@item.Url">@(item.GetPropertyValue<string>("pagetitle"))</a></li>
}
</ul>
}
Extra bonus info: you don´t need to int.TryParse when you hardcode the id. So you can do just:
@inherits Umbraco.Web.Macros.PartialViewMacroPage @{ // get IPublishContent object from id IPublishedContent parent = UmbracoContext.Current.ContentCache.GetById(1128);
// get children by documenttype "Module"
IPublishedContent[] children = parent.Children.Where(x => x.DocumentTypeAlias == "Module").ToArray();
}
Getting a VortoValue from a specific node
Hello guys,
I just started off with Razor and i am running into all kinds of trouble and i was hoping you could help me along my way. I always work with XSLT but for this project we decided to use Razor because of Vorto (specifically made for Multilingual sites).
I'm trying to do something really simple but i can't find any documentation on how to do it. I want to make a list with childpages from a specific page. How would i go around doing this by using id instead of the current selection i'm using. I also want to use the same selection to add the value of a field inside that specific node as h2.
Hi Lex,
you can get children from a specific page by using
You can get properties from child-node this way:
Question! Why are you using the sitem var instead af using the item? You could go:
I haven´t tested the code, but it schould work.
Regards René
Hi René,
Thank you for your reply. Could you show me how to put this all together, because the problem i keep having when constructing Razor is that i somehow do something wrong which makes my macro fail.
I am using the @sitem because of
Hi Lex,
i hardly use MacroPartials anymore. But i can try giving it a go. I presume you are using a macroparameter to set the id of the parent node.
/René
Hello René, i wasn't setting the id with a MacroParameter but as a hard value. So with a little bit of Googling i changed:
To:
Which works perfect. Thank you so much for your help René!
Hi Lex,
great you got it working! :-)
Extra bonus info: you don´t need to int.TryParse when you hardcode the id. So you can do just:
/René
is working on a reply...