Hello all, Umbraco is fairly new to me as is Razor, and I've had quite abit of fun learning these together over the past month or so.
My question is to do with the Razor syntax, see below for a line I wrote to pull back all children of the PromoRepository node:
var repo = @Model.AncestorOrSelf().PromoRepository.First().Descendants();
This returns and error, however the lines below work as expected:
var repo = @Model.AncestorOrSelf(); var promo = repo.PromoRepository.First(); var items = promo.Descendants();
Can someone explain to me why the first snippet of code throws an error, considering that essentially they are doing the same job.
Thank you very much, this is really an exercise in working out how Razor operates, although I know what works it would be great to know why it is the case.
Also if any one has tutorial suggestions that would also be greatly appreciated.
Go through these tutorials for Razor first , they are really good, it will help you building your Razor skills asuming your good at c#, and if you have any queries related to razor you can ask on http://www.stackoverflow.com or http://forums.asp.net/
Well to get all the childrens why dont you just use :
<ul>
@foreach(var item in @Model.Children()) OR if i am not wrong you can use @foreach(var item in @Model.DescendentsOrSelf()) Or @foreach (var item in Model.NodeById(1118).Descendants()) OR @foreach (var item in Model.AncestorOrSelf().Children.Where("NodeTypeAlias == \"PromoRepository \"").First().Children())
Thanks Mr A, I'm now working through the getting started with Razor tutorial. My C# isnt very strong at all, that said I'm not having any trouble with this tutorial so far.
Those foreach methods work nicely, its just good to see different ways of doing things. The .Where("NodeTypeAlias == \"PromoRepository \"") however doesnt work, I found Where.("NodeTypeAlias.Contains(\"PromoRepository\")\") to work thought.
The Contains operator is really useful and I'd like to know where I can find out more about this and other simialar operators , any idea where I may find a reference for these as they dont seem to be part of the Razor syntax
Thank you very much for your help! That cheat sheet is brilliant. It doesnt cover the "Contains" and similar operators though. That said its a massive help.
Stringing Razor syntax
Hello all, Umbraco is fairly new to me as is Razor, and I've had quite abit of fun learning these together over the past month or so.
My question is to do with the Razor syntax, see below for a line I wrote to pull back all children of the PromoRepository node:
var repo = @Model.AncestorOrSelf().PromoRepository.First().Descendants();
This returns and error, however the lines below work as expected:
var repo = @Model.AncestorOrSelf();
var promo = repo.PromoRepository.First();
var items = promo.Descendants();
Can someone explain to me why the first snippet of code throws an error, considering that essentially they are doing the same job.
Thank you very much, this is really an exercise in working out how Razor operates, although I know what works it would be great to know why it is the case.
Also if any one has tutorial suggestions that would also be greatly appreciated.
Have you gone through http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets
I was reading through it and finding it useful. Some aspects are a little difficult to understand as I'm not from a .NET background.
Go through these tutorials for Razor first , they are really good, it will help you building your Razor skills asuming your good at c#, and if you have any queries related to razor you can ask on http://www.stackoverflow.com or http://forums.asp.net/
http://www.asp.net/webmatrix/tutorials/1-getting-started-with-webmatrix-and-asp-net-web-pages
http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx
Well to get all the childrens why dont you just use :
<ul>
@foreach(var item in @Model.Children()) OR if i am not wrong you can use @foreach(var item in @Model.DescendentsOrSelf()) Or @foreach (var item in Model.NodeById(1118).Descendants()) OR @foreach (var item in Model.AncestorOrSelf().Children.Where("NodeTypeAlias == \"PromoRepository \"").First().Children())
{
<li><a [email protected] >@item.Name</a></li>
}
</ul>
Thanks Mr A, I'm now working through the getting started with Razor tutorial. My C# isnt very strong at all, that said I'm not having any trouble with this tutorial so far.
Those foreach methods work nicely, its just good to see different ways of doing things. The .Where("NodeTypeAlias == \"PromoRepository \"") however doesnt work, I found Where.("NodeTypeAlias.Contains(\"PromoRepository\")\") to work thought.
The Contains operator is really useful and I'd like to know where I can find out more about this and other simialar operators , any idea where I may find a reference for these as they dont seem to be part of the Razor syntax
Please find the cheat sheet on http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet
Thank you very much for your help! That cheat sheet is brilliant. It doesnt cover the "Contains" and similar operators though. That said its a massive help.
Also there is a good starter kit for razor lovers: http://our.umbraco.org/projects/developer-tools/cultiv-razor-examples
is working on a reply...