Clear ComboBox text when value has no match in datasource

I have a Customer object with a CountryCode property of type int that is used as foreign key for a Countries table.

The user is asked to select a country from a ComboBox, that I want to be initially empty.

When the page is loaded I create a Customer object, whose CoutryCode is set to 0, that means, no country code selected yet (in my Countries table I have no countries with Code = 0).

This is what happens to ComboBox text:

  • If CountryCode has a matching row in Countries, then then country name is shown. This is ok.
  • If CountryCode has a no matching rows in Countries (this happens when CountryCode = 0), then CountryCode value ("0") is shown. This is bad: I would like the placeholder (or an empty box) to be shown when an invalid value is selected.
  • If text is removed via keyboard then CountryCode is set to 0. This is ok.
In version 18 there used to be a text property in ComboBox that could be used force text content, but it sems it has been removed.

I'd like to stress that I'm aware that using int? instead of int for Customer.CountryCode will solve this issue. Unfortunately, this would impact other aspects of my code, so I cannot change this.

Thank you



Attachment: BlazorApp1_bee6429a.zip


3 Replies 1 reply marked as answer

PM Ponmani Murugaiyan Syncfusion Team October 6, 2021 12:57 PM UTC

Hi ROBERTO, 

Thanks for contacting Syncfusion support. 

We can reproduce the reported issue in our end. The cause for your reported query is, by default AllowCustom property is true for ComboBox component which allows to set the user defined value to the component even which does not exist in data source. So, if the CountryCode value ("0") is not present in the datasource, it consider as text and updated in the component. 

So, we suggest you to disable the AllowCustom property to get rid of this. Please find the modified sample below for reference. 


Please get back us if you need further assistance. 

Regards, 
Ponmani M 


Marked as answer

RR ROBERTO REALE October 7, 2021 04:45 PM UTC

Hi,


I set AllowCustom="false" and now it works as expected.


Thank you!

Roberto



BC Berly Christopher Syncfusion Team October 8, 2021 06:30 AM UTC

Hi ROBERTO REALE, 

We are glad to know that the issue is resolved at your end. Please let us know if you need further assistance on this. 

Regards, 
Berly B.C 


Loader.
Up arrow icon