Hi,
I am looking for guidance on the correct way to provide an empty option in a DropDownList bound to a collection of objects. It appears to work if you just use a 'null' defaultItem:
defaultItem={null}
which adds an empty item to the top of the list and returns a null value onChange. However, the documentation suggests that this property is supposed to be a placeholder and that the defaultItem property type must match the data type. Is there a better approach to adding an empty item to the list?
Kind regards,
David
Hi David,
The "defaultItem" is the correct way for enabling the DropDownList to have a "reset" value, although that I would recommend using a string that will help the end user to easily understand that they are going to reset the value (maybe "Reset value" or "Empty selection"). When you need to handle the value afterwards you can then check if the value matches the defaultItem string and if so, handle it as null.
If the DropDownList is bound to a collection with objects, you can either set the defaultItem to have text and null value or you can add such item directly in the data:
The above example demonstrates both approaches for handling objects.
Hope this helps.