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 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?
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:
Any ideas?
Thanks
You need to cast it or use the
method that casts it for you.
Or cast it yourself:
Hi Martin,
Thanks for the answer.
When I GetPropertyValue I get it like this:
Specify the type before the property alias. So this example would be like this:
Is there a different between these two or it's just a personal preference?
Regards,
Paulius
The first gets the raw object and tries to cast it to a string array. If it fails the lineup variable is null.
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.
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
Hi Paulius,
Did you get empty lines as well, when looping through the array, if yes, how did you fix it?
Regards, Allan
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:
so
type
is of typeString
, notString[]
.What am I doing wrong?is working on a reply...
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.