Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    May 02, 2012 @ 11:23
    Anthony Candaele
    0

    How to access values stored with MNTP in Razor

    Hi,

    I'm using uComponents Multi-Node Tree Picker for the first time. I have a custom datatype 'Promotors' that stores promotors:

    My Razor-script to render the promotors looks like this:

    @if (Model.HasValue("Promotors"))
    {
     <p>Promotors:
       @foreach (var promotor in @Model.Promotors)
       {
        @promotor.InnerText
       }
     </p>
    }

    However, this only renders the id of the stored promotor

    How can I render the value of the stored promotor?

    Thanks for your help,
    Anthony

  • gilad 185 posts 425 karma points
    May 02, 2012 @ 11:34
    gilad
    1

    Hi Anthony.

    you have to get the node Object by the id and then you can acsses any property of this node...

    @foreach (var promotor in @Model.Promotors)
       {
    dynamic node = new DynamicNode(
    promotor.InnerText);
    <span>@node.Name</span> /// or - @node.properyAlias   
       }

    Hope that help. cheers.

  • Anthony Candaele 1197 posts 2049 karma points
    May 02, 2012 @ 11:40
    Anthony Candaele
    0

    Hi gilad,

    Yes that helps :)

    Tweaked my code a little:

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext

    <h1>@Model.Name</h1>
    @if (Model.HasValue("Promotors"))
    {
     <p>Promotors:
       @foreach (var id in @Model.Promotors)
       {
        dynamic promotor new DynamicNode(id.InnerText);
        @promotor.Name
       }
     </p>
    }

    This works.

    Thanks a lot for your help,
    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    May 02, 2012 @ 11:59
    Anthony Candaele
    0

    I tweaked the code a little further. When there are more promotors stored, it will add a ',' between each promotor, except the last promotor:

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext

    <h1>@Model.Name</h1>
    @if (Model.HasValue("Promotors"))
    {
     <p>Promotors:
       @foreach (var id in @Model.Promotors)
       {
        dynamic promotor new DynamicNode(id.InnerText);
        if (id.IsLast())
        {
         @promotor.Name
         }
        else
        {
         @(@promotor.Name " , ");
         }
       }
     </p>
    }


    This renders something like:

    Promotors: John Doe, Jane Doe, Baby Doe

  • gilad 185 posts 425 karma points
    May 02, 2012 @ 12:03
    gilad
    0

    Great!

    you can do it little bit nicer like this :

     <p>Promotors:
       @foreach (var id in @Model.Promotors)
       {
        dynamic promotor new DynamicNode(id.InnerText);
        @promotor.Name 
        if (!id.IsLast())
        {
           @Html.Raw(" , ");
        }
       }
     </p>

    but it is same result.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies