We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Error on AutocompleteFor get key

Hi,

I've an autocomplete inside a form. I need the ID from the autocomplete text, but it always return null.

I've attach an example, with a dropdownlistfor that the selected item is binding ok, but the autocomplete not returns the ID

Attachment: Autocomplete_eb5bb22e.zip

5 Replies

AP Arun Palaniyandi Syncfusion Team January 31, 2017 10:19 AM UTC

Hi Manolo,   
   
Thanks for contacting Syncfusion Support.   
   
Based on the analysis of your shared code, we found that you had sent the key value (i.e., ID) to the controller. Hence, in your Autocomplete you could not directly send the key value to the code behind. If you want to get the key in controller directly using the Autocomplete ID, then you have to define a hidden input element and set its value on Select event. Refer to the below code sample.    
   
[CSHTML]      
   
@using (Html.BeginForm())   
{   
    <div class="row">   
        <input type="hidden" id="Auto_hidden" name="Auto" />       
        @Html.EJ().AutocompleteFor(x => x.IdCar, newSyncfusion.JavaScript.Models.AutocompleteProperties()   
{   
    DataSource = (IEnumerable<CarsList>)ViewBag.datasource,   
    AutocompleteFields = new AutocompleteFields() { Text = "text", Key = "uniqueKey"}   
}).Width("100%").ClientSideEvents(c => c.Select("onSelect"))      
   
    </div>   
[Script]       
  function onSelect(args) {   
            $("#Auto_hidden").val(args.key);   
        }   
   
[Controller]    
   
[HttpPost]   
        public ActionResult AutocompleteFeatures(CarVM model,string Auto)   
        {   
   
            int Id = Int32.Parse(Auto);      
            return View(model);   
        }   
  
    
   
      
Please let us know if you have any more queries.        
     
Regards,       
Arun P.    



MA Manolo January 31, 2017 11:56 AM UTC

Hi,

I think that Autocompletefor not bind correctly. When I use this control, the key must be binded with the model property automatically. Similar at dropdownlistfor.

Also, if I've set the value for the model property, autocompletefor show the key, not the text.

I've modified the example, and I put also a dropdownlistfor, that it works perfectly, getting ans setting the property model value



Attachment: Autocomplete_8e68ef2.zip


AP Arun Palaniyandi Syncfusion Team February 1, 2017 11:53 AM UTC

Hi Manolo,    
    
Thanks for the update. 
 
As updated earlier, in Autocomplete you could not directly send the key value to the code behind. But in the DropDownList, we are having the text and value property, so that value property can be obtained in controller since that will be stored in the hidden input. 
We have also checked your sample and you have passed the key in the controller but it shows the text only in view not the key. Since this is the behavior of the Autocomplete, we have to manually pass the key to the controller. 
 
If you need not like to pass the key value manually, we suggest you to use the DropDownList with the EnableFilterSearch to behave that will allow you to search like Autocomplete.
 
Please let us know if we have any queries further. 

Regards, 
Arun P. 



MA Manolo February 2, 2017 09:27 AM UTC

Ok, is better use drop down list.

Thanks for all


AP Arun Palaniyandi Syncfusion Team February 3, 2017 12:33 PM UTC

Hi Manolo,     
     
Thanks for the update. 
 
We are glad that our suggestion helped you. 
 
Please let us know if you have any queries in future. 

Regards, 
Arun P. 


Loader.
Live Chat Icon For mobile
Up arrow icon