AutoComplete selected value?

Hi there, how do you get the selected value of an AutoComplete?
It seems I can only get the Text that was typed in the box. But I'm trying to get an Id value of a row loaded in the autocomplete's list of items. I was hoping it would work like a dropdown.
I see it has a ValueExpression property, but there's no documentation on it, it appears, and I can't tell what it does. I tried using the Change event a couple different ways:

  • ((args) => LoadSelectedItem(args))
  • LoadSelectedItem

But it's still giving me only the text that was typed -- not anything about the underlying selected item.
I could make it work if there were a int SelectedIndex property, for example. Just having the text that was typed in the box doesn't really help me, however.

The Value of the AutoComplete is always the text that the user typed or the value of the TextProperty of the selected item. Since the AutoComplete allows for custom values (not available in the suggestion box) it cannot provide the whole data item as a value. There is nothing to provide if the user entered some text which does not exist in the suggestions.

You have two options

  • just find the item by text e.g. var item = data.Where(o => o.Name == autoCompleteValue).FirstOrDefault()
  • Use RadzenDropDown with filtering. As long as you don't set ValueProperty you will get the "whole" data item as Value.

I agree if the user types something not in the list, there's no Value to return.
But if there is a certain item the user selected, I feel like that should be known.
The use case here is as a general-purpose search box.
In my case, I have many thousands of names, emails, and phone numbers. An AutoComplete is a good case for this rather than a dropdown.
If I search for "abbot", I get some results to appear. There are id numbers behind these results that I need. I think you're telling me there's no way to know what the user selected.
The value "abbot" is available in the event handler (what the user typed), but this doesn't uniquely identify something in the list. Do you know a better way to do what I'm trying to do?

image

My second reply is to try RadzenDropDown with filtering enabled.

This would be very, very helpful. Any new plans around this issue?

Hi, looking at the RadzenAutoComplete source code ( the SelectItem Method) i came out with a workaround.
I didn´t specified TextProperty, override the ToString() method of the source class and used the Value Property and the Onchange event in this way:

<RadzenAutoComplete Value=@PaisSelected Data=@paises Change=@OnChange LoadData=@Buscar_Paises />

(you may translate PaisSelected to SelectedCountry, paises to countries and Buscar_Paises to Search_Countries)

Then the onchange event like this:

        void OnChange(dynamic args)
        {
            if (args.GetType().Name.Contains("Pais"))
                PaisSelected = args;
            else
                PaisSelected = new();
        }

Do you think i can use this without concern or could there be breaking changes in the future?

Thank you!

We don’t plan any breaking changes to AutoComplete component.

1 Like

I'm having the same issue and would like to implement your solution. Can you provide the code that shows all the changes you needed to make to get your solution working?

Thank you.