IPublishedContent.Value() with defaultValue doesn't return defaultValue
See original GitHub issueI upgraded my Umbraco instance from 7.15 to 8.1.3 which was successful. Though, one particular section of the site started to malfunction due to a changed behavior in 7 vs 8. The defaultValue isn’t returned anymore as it was before. An empty string is returned in my case for a dropdown list. For a decimal field, 0 is returned instead of the defaultValue.
Reproduction
If you’re filing a bug, please describe how to reproduce it. Include as much relevant information as possible, such as:
Bug summary
When using IPublishedContent.Value()
with the defaultValue parameter in Umbraco 8, the default value isn’t being returned for dropdown list and decimal editor.
An empty string is returned in my case for a dropdown list. For a decimal field, 0 is returned instead of the defaultValue.
When using IPublishedContent.GetPropertyValue()
with a defaultValue parameter in Umbraco 7 , the specified defaultValue is returned.
Specifics
- Umbraco 8.1.3
Steps to reproduce
- Setup dropdownlist editor with multiple options
- Create content with dropdown list editor, but don’t select a value
- Query the content
- Use the
IPublishedContent.Value()
with the defaultValue parameter
Expected result
The specified defaultValue should be returned.
Actual result
An empty string is returned.
Snippet
// Umbraco 7 code
private static void Test(IPublishedContent content){
var category = content.GetPropertyValue<string>("category", "none") // dropdown editor
Console.WriteLine(category); // output is "none"
}
// Umbraco 8 code, not working as expected
private static void Test(IPublishedContent content){
var category = content.Value<string>("category", defaultValue: "none"); // dropdown editor
Console.WriteLine(category); // output is "", an empty string
}
// Umbraco 8 code, working as expected
private static void Test(IPublishedContent content){
var category = content.HasValue("category") ? content.Value<string>("category") : "none"; // dropdown editor
Console.WriteLine(category); // output is "none"
}
As you can see, using the IPublishedContent.HasValue()
is a working workaround.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (8 by maintainers)
@kjac thank you for providing a link to the documentation.
With the docs referenced and situation explained, I believe we can close this issue. Hopefully the ticket will save some time of other people upgrading and running into this.
Thank you all! 🙏
The problem is with… what’s a value and what’s not a value. For a string, passing a non-null string as
defaultValue
probably means that we shouldFallback.ToDefaultValue
. But for an integer… how do we make a difference between passing0
explicitely as a default value, and0
beingdefault(int)
as in, no default value was specified?Remember that in C# we have no way to know whether a parameter was passed for real, or has its default value.
Now… maybe we could define
.Value<T>(string, T)
overloads that would do exactly what you want. Cannot remember if it wasn’t done because it opens more problems - or because I forgot. Let me check.