Copied to clipboard

Flag this post as spam?

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


  • Mike 3 posts 23 karma points
    Oct 03, 2013 @ 19:18
    Mike
    0

    Razor get childnode properties umbraco item

    Dear people,

    First off, thank you for reading, second I have never used razor so I'm still learning.

    The issue:
    I want to create my own "contact form" that can be managed from the back-end of Umbraco for the users to add/edit/remove fields. The first problem is, I can't find any good packages that full fill my requirements. So, I want to create my own "contact form" by using the Umbraco macro script functionality.

    My solution:

    1. I create a document type with properties added (without template).
    2. I add my macro script to the specified node.
    3. I add a subnode based on the document type earlier.


    Now I want to read the properties of the subnode so I can dynamiclly create the form.

    The problem:
    Razor doesn''t show any error messages other than:

    Error loading Razor Script

    To solve this I tried to changed the web.config file to set debug from false to true and add querystring "?umbDebugShowTrace=true". But this doesn't work.

    After testing line by line I noticed the error is caused by this line of my chtml code:

    <umbraco:Item field="@property.Alias" nodeId="@settingsNode.Id" runat="server" />

    Complete code:

    @using System.Web
    @using System.Web.Security
    @using umbraco.NodeFactory
    @using umbraco

    @functions {
    private umbraco.NodeFactory.Node getSettingsNode(umbraco.NodeFactory.Node currentNode)
    {
    foreach (Node child in currentNode.Children)
    {
    if (child.NodeTypeAlias.Equals("ContactFormSettings"))
    {
    return child;
    }
    }

    return null;
    }
    }

    @{

    var settingsNode = getSettingsNode(umbraco.NodeFactory.Node.GetCurrent());

    foreach (Property property in settingsNode.Properties)
    {
    <umbraco:Item field="@property.Alias" nodeId="@settingsNode.Id" runat="server" />

    }

    }

    Question:
    How can I show the error message?
    What am I doing wrong here? Isn't this suppose to work?

    Thanks in advance,

    Mike


  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 20:46
    Rich Green
    0

    Hey Mike,

    Welcome to the forum!

    This line here should only be use in templates

    <umbraco:Item field="@property.Alias" nodeId="@settingsNode.Id" runat="server"/>

    As you are in Razor, you can do this:

    var settingsNode = getSettingsNode(umbraco.NodeFactory.Node.GetCurrent());

           
    foreach(Property property in settingsNode.Properties)
           
    {
               
    <p>
    @property.Alias</p>

           
    }

    Hope that helps

    Rich

  • Mike 3 posts 23 karma points
    Oct 03, 2013 @ 21:42
    Mike
    0

    Dear Rich Green,

    Thank you for your quick response. The solution you specified does work, however I want to show the element itself.

    For example:
    If there is a property with type "Checkbox" I want to show a checkbox, not the Alias.
    If there is a propery with type "Textbox" I want to show a textbox.

    I hope I made myself clear enough for you to understand.

    Mike

     

  • Rich Green 2246 posts 4008 karma points
    Oct 03, 2013 @ 22:14
    Rich Green
    0

    Hi Mike,

    There's no magic way to do this.

    I'm assuming you have different DocTypes for checkbox and textbox?

    So your code would be something like:

     

    var settingsNode = getSettingsNode(umbraco.NodeFactory.Node.GetCurrent());

           
    foreach(Property property in settingsNode.Properties)
           
    {
               
    if(property.NodeTypeAlias=="checkBoxDocType")
    { //checkbox}elseif(property.NodeTypeAlias=="textboxDocType"){///textbox}

           
    }

    Though I'm not sure why you don't just use Umbraco Contour?

    Rich

  • Mike 3 posts 23 karma points
    Oct 04, 2013 @ 11:16
    Mike
    0

    Dear Rich Green,

    Oh well, it was just a theory. I know about the Contour package, but I have a cheap budget and I like to build it myself. I solved the problem by using a static (un-changeable from back-end) contact form, it will be good for now.

    Thanks for the help!

    Mike

Please Sign in or register to post replies

Write your reply to:

Draft