Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thomas Boyd 10 posts 30 karma points
    Nov 01, 2011 @ 10:40
    Thomas Boyd
    0

    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.

  • Mr A 216 posts 278 karma points
    Nov 01, 2011 @ 10:44
  • Thomas Boyd 10 posts 30 karma points
    Nov 01, 2011 @ 10:47
    Thomas Boyd
    0

    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.

  • Mr A 216 posts 278 karma points
    Nov 01, 2011 @ 10:55
    Mr A
    0

    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

  • Mr A 216 posts 278 karma points
    Nov 01, 2011 @ 11:37
    Mr A
    0

    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>

     

  • Thomas Boyd 10 posts 30 karma points
    Nov 01, 2011 @ 12:03
    Thomas Boyd
    0

    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

  • Mr A 216 posts 278 karma points
    Nov 01, 2011 @ 12:10
  • Thomas Boyd 10 posts 30 karma points
    Nov 01, 2011 @ 12:26
    Thomas Boyd
    0

    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.

  • Mr A 216 posts 278 karma points
    Nov 01, 2011 @ 13:24
    Mr A
    0

    Also there is a good starter kit for razor lovers: http://our.umbraco.org/projects/developer-tools/cultiv-razor-examples

Please Sign in or register to post replies

Write your reply to:

Draft