Copied to clipboard

Flag this post as spam?

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


  • Allan49 35 posts 55 karma points
    Sep 28, 2011 @ 17:18
    Allan49
    0

    How to list what is in a Razor Model

    Hi,

    Is there any way to get a hierarchical list of what is inside a Model?

    I am looking for in razor is something equivalent with <xsl:copy-of select="." /> or xmldump project in xslt territory.

    Thank you

  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Sep 28, 2011 @ 17:20
    Sebastiaan Janssen
    0

    I've done it before by creating a JSON string out of it, with a little creativity you can mold it into anything you want: 

    http://cultiv.nl/blog/2011/7/25/razor-vs-base-to-output-json-in-umbraco/

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 28, 2011 @ 21:34
    Dan Diplo
    1

    I achieved this by writing a quick razor helper that dumps all the properties out as a table. You can do this by creating a .cshtml file in App_Code folder. I called mine DiploHelpers.cshtml Inside it create a function like this:

    @helper DumpProperties(umbraco.MacroEngines.DynamicNode node)
    {
        var items = node.PropertiesAsList;
    
        <table width="100%" border="1" cellpadding="4" style="background-color:#eee;font-size:smaller">
            <tr style="background-color:#000;color:white">
                <th>Alias</th><th>Value</th><th>Type</th>
            </tr>
    
            @foreach (var item in items)
            {
                <tr>
                    <td>@item.Alias</td><td>@item.Value</td><td>@item.Value.GetType()</td>
                </tr>
            }
    
        </table>    
    
    }

    Then you can call this from your Razor file like so:

    @DiploHelpers.DumpProperties(Model);

    Of course, you can pass in any Node (not just the current Model and including Media nodes) to the helper.

  • Allan49 35 posts 55 karma points
    Sep 28, 2011 @ 23:23
    Allan49
    0

    Hi Dan,

    Thank you for reply. I guess your code doesn’t cover the hierarchy between the Model’s children and grandchildren.

     Thanks,

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 28, 2011 @ 23:45
    Dan Diplo
    1

    Well, you wouldn't always want to iterate over all descendent nodes, as that could mean iterating over a lot of nodes. But it would be easy enough to make the code recursive, if that is what you wanted.

  • Mark 14 posts 84 karma points
    Sep 24, 2019 @ 13:53
    Mark
    0

    Dan, What if you do not have an App_code folder? Does it matter where the helper lives?

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 24, 2019 @ 19:37
    Dan Diplo
    0

    You'd probably need to create an HtmlHelper method if you wanted it to be compiled as part of your solution.

    See https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs

    https://www.foreach.be/blog/create-a-custom-html-helper-in-asp-net-mvc-using-razor

    Or I guess you could also use an HTML Partial class and pass in your model to it.

  • 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