How to bind ComboBox/DropDownList to model (object) instead of primitive type

1 Answer 122 Views
ComboBox DropDownList
Doug
Top achievements
Rank 1
Iron
Veteran
Doug asked on 27 Apr 2021, 10:35 PM
The combo box data binding documentation discusses the ability to bind to a model, however in the example the bind-Value is set to an integer property. If I set the bind-Value to a property that is an object type I get an invalid cast exception. Is there a way to bind such that the selected value is an object instead of a primitive type? I know I can use a primitive type as the selected value and then fetch the object out of the list in my view model but it would be cleaner to bind directly to the object itself.

1 Answer, 1 is accepted

Sort by
0
Hristian Stefanov
Telerik team
answered on 29 Apr 2021, 01:05 PM

Hi Doug,

The ComboBox/DropDownList provides a primitive Value so that the validation can work, and so that other data source operations (such as filtering) can work. The Value and Text cannot be classes (models) because that would prevent validation from working, and filtering/comparing entire classes is an operation that is not defined.

Your way is the best and the easiest possible to achieve the desired result. Using the unique identifier provided from the component (the Value) and fetching the selected object out of the list. You can find examples of doing that in the following article: Get model from dropodwn

Regards,
Hristian Stefanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Doug
Top achievements
Rank 1
Iron
Veteran
commented on 11 Oct 2021, 05:23 PM

Hristian,

I might suggest you take a look at Chris Sainty's Blazored Typeahead control. It allows @bind-Value to be a non-primitive type and yet validation still works. It works a little differently than the Telerik drop down controls but to me it's more intuitive so I just want to throw it out there as food for thought.

https://github.com/Blazored/Typeahead

 

Hristian Stefanov
Telerik team
commented on 14 Oct 2021, 01:28 PM

Hi Doug,

Thank you for sharing with us your thoughts on this case.

We highly value the feedback we receive for the behavior of our components.

Regards,
Hristian Stefanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ComboBox DropDownList
Asked by
Doug
Top achievements
Rank 1
Iron
Veteran
Answers by
Hristian Stefanov
Telerik team
Share this question
or