Copied to clipboard

Flag this post as spam?

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


  • Paulius Putna 78 posts 136 karma points
    Mar 07, 2016 @ 12:45
    Paulius Putna
    0

    Repeatable textstrings

    Hi all,

    I just can't figure how to get values from Umbraco core editor Repeatable textstrings.

    When I use @Model.GetPropertyValue("lineup") it returns System.String[], but when I try to loop, get Length or do any actions it throughs an error:

    CS1579: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
    

    Any ideas?

    Thanks

  • Martin Almström 33 posts 159 karma points
    Mar 07, 2016 @ 13:00
    Martin Almström
    101

    You need to cast it or use the

    @Model.GetPropertyValue<string[]>("lineup") 
    

    method that casts it for you.

    Or cast it yourself:

    var repeatableString = Model.GetPropertyValue("lineup") as string[];
    if(repeatableString != null && repeatableString.Length > 0){do stuff here}
    
  • Paulius Putna 78 posts 136 karma points
    Mar 07, 2016 @ 14:14
    Paulius Putna
    0

    Hi Martin,

    Thanks for the answer.

    When I GetPropertyValue I get it like this:

    var coverImageId = Model.GetPropertyValue<int>("coverImage");
    

    Specify the type before the property alias. So this example would be like this:

    var lineup = Model.GetPropertyValue<string[]>("lineup");
    

    Is there a different between these two or it's just a personal preference?

    Regards,

    Paulius

  • Martin Almström 33 posts 159 karma points
    Mar 07, 2016 @ 14:28
    Martin Almström
    0

    The first gets the raw object and tries to cast it to a string array. If it fails the lineup variable is null.

    var lineup = Model.GetPropertyValue("lineup") as string[];
    

    The generic method type does a more hard type cast. So if the property contains an integer this line of code will cast an exception.

    var lineup = Model.GetPropertyValue<string[]>("lineup");
    
    //These two do the same thing. Type Cast Exception will be thrown if the property isn't of the correct type.
    
    var lineup = (string[])Model.GetPropertyValue("lineup");
    

    I use the second one as its easier to read and you don't have to check for null and it's easy to read, and I rarely change the document types.

    I like that I get the exception

    But they eventually do the same thing :)

    Hope that help :)

    Regards

    Martin

  • Allan Lange 6 posts 96 karma points
    Apr 12, 2016 @ 13:21
    Allan Lange
    0

    Hi Paulius,

    Did you get empty lines as well, when looping through the array, if yes, how did you fix it?

    Regards, Allan

  • Aziz Khakulov 9 posts 79 karma points
    Sep 20, 2016 @ 09:26
    Aziz Khakulov
    0

    I have created a property of type Repeatable Text Strings but when I try to access it in code via var myVar = Model.GetPropertyValue<string[]>("myVar"); myVar is null.

    If I do following in code:

    var myVar = spotifySettings.GetValue("myVar");
    var type = myVar.GetType();
    

    so type is of type String, not String[].What am I doing wrong?

  • 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