Copied to clipboard

Flag this post as spam?

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


  • Eddie Foreman 215 posts 288 karma points
    Nov 25, 2013 @ 22:57
    Eddie Foreman
    0

    Using Node API with Textstring Array

    Hi,

    I have a usercontrol with an update panel which displays filtered product information.  Some of the values are created in Umbraco using the Textstring array.  Below is a the generated xml for the Textstring Array:

                    <previousAERRates>
    <TextstringArray>
    <values>
    <value>3.35%</value>
    <value>09/06/13</value>
    </values>
    <values>
    <value>2.95%</value>
    <value>22/12/11</value>
    </values>
    </TextstringArray>
    </previousAERRates>

    As I'm displaying other properties on the page I've created a node object and have been using the GetProperty method.  This is fine for the product name etc...  But I not usre if I can use the same approach with the Textstring Array. 

    Code extract:

    var node = new Node(Convert.ToInt16(ddl.SelectedValue));//Selected node ID via drop down menu on web site page
    //display other property valuse
    //..
    string aerValues = node.GetProperty("previousAERRates").Value;
    //Iterate over value pairs and display on page

    Would be grateful if someone could advise on how I can get the XML so that I can display each set of values in the update panel.

    Best Wishes.

    Eddie

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 26, 2013 @ 09:43
    Lee Kelleher
    100

    Hi Eddie,

    Which version of uComponents and which type of Razor are you using? (e.g. macroScript or MVC/partial)

    For the Razor macroScripts we have a custom model-binder that should be used automatically, this should give you the value as an List<string[]> object type. You can loop over that accordingly.

    With MVC Razor, we have a PropertyEditorValueConverter that will do the same thing, but it's only available in uComponents v6+ (supports Umbraco 6.1+)

    Cheers, Lee.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Nov 26, 2013 @ 12:36
    Jeroen Breuer
    0

    Since you also have 2 values in your Textstring Array you could use something like this:

    private IEnumerable<ProductProperty> GetProperties(IPublishedContent content)
    {
        var xml = content.GetPropertyValue<XElement>("properties");
    
        if (xml == null)
        {
            return Enumerable.Empty<ProductProperty>();
        }
    
        var properties =
        (
            from x in xml.Descendants("values")
            let elements = x.Elements()
            select new ProductProperty()
            {
                Text = elements.ElementAt(0).Value,
                Value = elements.ElementAt(1).Value
            }
        );
        return properties;
    }

    Jeroen

  • Eddie Foreman 215 posts 288 karma points
    Nov 26, 2013 @ 14:13
    Eddie Foreman
    0

    Hi Lee,

    Sorry, I'm using Umbraco 4.11.10 and uComponents 5.5.0.  So have a usecontrol in a webform and then using the code behind to display the various property values.  With this version could you advise on the references that I need to add to the usercontrol project.

    Jeroen - thanks for your solution, will give this a try if I can't access the custom model-binder that lee mentioned.

    Thanks,
    Eddie

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 26, 2013 @ 17:45
    Lee Kelleher
    0

    Hi Eddie,

    No problem-o, usercontrols are fine.

    Once you get back the value from node.GetProperty("previousAERRates").Value you should have an XML string. You could then pass it to a function that will return you something more usable.

    e.g. code snippet taken from the Razor model-binder

    var values = new List<string[]>();
    
    if (!string.IsNullOrEmpty(yourValueFromPreviousAERRates))
    {
        var xml = new XmlDocument();
        xml.LoadXml(yourValueFromPreviousAERRates);
    
        foreach (XmlNode node in xml.SelectNodes("/TextstringArray/values"))
        {
            var value = new List<string>();
            foreach (XmlNode child in node.SelectNodes("value"))
            {
                value.Add(child.InnerText);
            }
    
            values.Add(value.ToArray());
        }
    }
    

    From there, you can do as Jeroen mentions, loop through the list and output the first and second values.

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft