question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IPublishedContent.Value() with defaultValue doesn't return defaultValue

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Swimburgercommented, Aug 27, 2019

@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! 🙏

1reaction
zpqrtbnkcommented, Aug 27, 2019

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 should Fallback.ToDefaultValue. But for an integer… how do we make a difference between passing 0 explicitely as a default value, and 0 being default(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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

default values for nested content fields
I am using Nested Content and I want to set 2 of the fields ("Entered By" and "Entered Date") to a default value...
Read more >
IPublishedContent Property Access & Extension Methods
Built-in properties, which exists on all content objects by default. Common Examples ... Returns the unique Id for the current content item. @Model.Id ......
Read more >
DefaultValue not working | Optimizely Developer Community
It is an existing Block, existing properties [DefaultValue] is working fine. I added a new property and it won't set the default value....
Read more >
Umbraco Document Type Field Default Value
I would like set a default value for a date picker field in a Document Type in Umbraco. How can I do this?...
Read more >
Why do we have default values for class properties?
I get why we are declaring the properties, but do not understand why we are giving them default values. class Product { public...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found