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

How to get the selected item ID(KEY) from Autocomplete

Hello ! 

the scafolding gave me this : 
@Html.DropDownList("Id_Vehicule", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Id_Vehicule, "", new { @class = "text-danger" })

and i replace it with the Syncfusion Autocomplet :
                @Html.EJ().Autocomplete("Id_Vehicule").Width("280").Datasource((IEnumerable<Vehicule>)ViewBag.datasource).AutocompleteFields(f => f.Key("Id_Vehicule").Text("Immatriculation")).HighlightSearch(true).FilterType(FilterOperatorType.Contains).ShowPopupButton(true).EmptyResultText("Véhicule introuvable")

My controller : 
List<Vehicule> v = new List<Vehicule>();
/* ... Adding items code  ...*/
 ViewBag.datasource = v.ToList();

--- in the view Side the Autocomplete works fine.
--- in the controller Side i cant get the Value .

So! how can i get it ? 
Thank you !

13 Replies

ES Ezhil S Syncfusion Team April 23, 2015 10:10 AM UTC

Hi Djamel,
Sorry about the inconvenience caused.

Currently we haven’t provided support to “Return the selected item’s key to controller in Autocomplete” and fix for this issue will available in our upcoming service pack release Volume 1,2015, which is expected to be rolled out at the end of this month. We will let you know once the service pack release has been rolled out. We appreciate your patience until then.

Please let us know if you have any concerns. We will be happy to help you out.
Regards,
Ezhil S


DA David July 22, 2015 08:28 AM UTC

Hi!

we have same issue... some notice? some patch?

My version: 13.1.0.21


Thanks!


KC Kasithangam C Syncfusion Team July 23, 2015 02:11 PM UTC

Hi Djamel,


We have prepared the sample based on get the selected item ID(key) value in controller side and please find the sample under the following location,


Sample: Autocomplete Sample


In the above sample, we have passed the selected item ID to controller in the button click event as shown below,


<code>

<script>

  

    function OnClick(args)

    {

        var val = $("#selectcar").val();

        var obj = $("#selectcar").data("ejAutocomplete");

       var source= obj.option("dataSource");

        for(var i=0;i<source.length;i++)

        {

            if (source[i].text == val) {

                var id = source[i].uniqueKey;

                break;

            }

        }

        $.ajax({

            type: "Post",

            url: "result",

            data: { "id": id },

            success: function (result) {

                alert(result)

            },


        });

            }

</script>


</code>


In the controller page,you can get the id of the item as shown below code,

<code>

public ActionResult result(string id)

        {

            ViewBag.Message = id;

            return PartialView("_Partial1");


        }


</code>

Kindly let us know if you have further queries.

Regards,

Kasithangam



MS Matthew Shakespeare March 24, 2016 12:46 PM UTC

Has "Return the selected item’s key to controller in Autocomplete" become part of default functionality, I'm unable to return the id to my controller.

I'm using the autocomplete control as part of a larger form with multiple autocomplete controls, and i can't see a 'Nice' way of handling it even in sample you posted 


MS Matthew Shakespeare March 24, 2016 12:51 PM UTC

I'm unable to return the id to my controller*


SN Sasikala Nagarajan Syncfusion Team March 26, 2016 05:17 AM UTC

Hi Matthew,

Thanks for contacting Syncfusion support,

We have checked with your requirement. We would like let you know that, we have provided your reported requirement in our latest releases (from 13.4.0.58 Volume 4, 2015 SP1). You can get the key of selected items in form post back by setting the mulitiSelect Mode as “visual mode“. Please refer the below code example,

    @Html.EJ().Autocomplete("selectState").MultiSelectMode(MultiSelectModeTypes.VisualMode).Datasource((IEnumerable<object>)ViewBag.state).AutocompleteFields(f => f.Text("countryName").Key("index"))

[HttpPost]

        public ActionResult GetData(string selectState)

        {

            return View("Index");// from selectState you can get the keys of selected items

        }


If still you are facing any difficluites please provide more information about requirement along with your current EJ version which will help us to provide you the better solution,

Please check with the given solution and let us know if you have further queries,

Regards,

Sasikala Nagarajan


MS Matthew Shakespeare March 26, 2016 01:43 PM UTC

Thank you Sasikala, That resolves my issue


MS Matthew Shakespeare March 27, 2016 04:46 PM UTC

This sorts out my issue of your autocomplete control not returning the selected Item's Id, however by making it a multi-select control to resolve the issue it makes it unusable.
Looking at what you said again i think this might be due to a bad translation "selected item's id" vs "selected items"

I'm currently using the syncfusion.AspNet.Mvc5 nuget package version 13.4.0.63




KC Kasithangam C Syncfusion Team March 28, 2016 01:12 PM UTC

Hi Matthew,

Currently, we don’t have inbuilt functionality for when select the autocompelete item (not multiselect mode) and get the key of selected item in code behind. We have logged defect report for this. A support incident to track the status of this issue has been created under your account. Please log on to our support website to check for further updates.


https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents  


Looking at what you said again i think this might be due to a bad translation "selected item's id" vs "selected items"

Can you please explain clearly, you are going to get the selected item id (key alone) or selected items (key and text) in the code behind?


Please let us know if you have any query.


Regards,

Kasithangam



MS Matthew Shakespeare March 29, 2016 02:50 PM UTC


Can you please explain clearly, you are going to get the selected item id (key alone) or selected items (key and text) in the code behind? 

The id of the selected item 


KC Kasithangam C Syncfusion Team March 30, 2016 06:09 AM UTC

Hi Matthew,

As said in the previous update, we have logged a defect report for the issue “when select the autocompelete item, can’t able to get the key (id) of selected item in code behind”.We will update you once the issue has been fixed. We appreciate your patience until then and also feel free to get back to us if you need any further assistance.


Regards,

Kasithangam



SM Sean McGurk January 9, 2017 11:37 AM UTC

Hi,

Any update on this issue?

I am trying to use the key value for the selected item in my postback (rather than the selected text) and am unable to do so with the Autocomplete control.

Thanks,

Sean



AP Arun Palaniyandi Syncfusion Team January 10, 2017 11:36 AM UTC

 
Hi Sean,   
   
Thanks for contacting Syncfusion support.   
   
In the Autocomplete, we can get the selected item’s key value in controller by 2 ways.   
1.Using select event parameter   
2.Using selectValueByKey property   
   
After getting the key value, we need to send this to code behind by using AJAX. Please find the below code snippet for your reference.   
   
1.Using select event parameter   
   
Cshtml   
  
@Html.EJ().Autocomplete("selectcar").Width("205").Datasource((IEnumerable<CarsList>)ViewBag.datasource)   
.AllowGrouping(false).HighlightSearch(false).AutocompleteFields(f => f.Text("text").Key("uniqueKey")).ClientSideEvents(e => e.Select("onselect"))   
  
<script>   
        function onselect(e) {   
            keyvalue = e.key;   
            $.ajax({   
                type: 'POST',   
                url: '@Url.Action("AutoFeaturesnew""Autocomplete")',   
                data: {   
                    type: keyvalue   
                },   
                success: function (data) {                      
                    alert("key is suucessfully got in controller, Key:"+ data);   
                }   
            });   
    }   
    </script>   
  
Controller   
  
public JsonResult AutoFeaturesnew(string type)   
        {   
   
            return Json(type);   
   
        }   
   
2.Using selectValueByKey property   
   
After selected a text, the selected item’s key value will be maintained in the selectvaluebykey property.   
   
   
Cshtml   
  
<form>   
           @Html.EJ().Autocomplete("selectauto").Width("205").Datasource((IEnumerable<CarsList>)ViewBag.datasource).MultiSelectMode(MultiSelectModeTypes.None).AllowGrouping(false).HighlightSearch(false).AutocompleteFields(e => e.Text("text").Key("uniqueKey"))   
            <br/>   
            <input id="submit" type="submit" value="Submit" />   
        </form>   
<script>   
$('form').submit(function () {   
            var ac = $("#selectauto").ejAutocomplete("instance");   
            $.ajax({   
                url: '/autocomplete/AutocompleteFeaturesnew',   
                data: { val: ac.model.selectValueByKey },   
                type: 'POST',   
                dataType: "json",   
                success: function (data) {   
                    alert("key is suucessfully got in controller through form submit, Key:" + data);   
                }   
   
            });   
   
        });    </script>   
  
Controller   
  
public JsonResult AutocompleteFeaturesnew(string val)   
        {   
            return Json(val);    
        }    


 
 



 
 
  
Regards,   
Arun P   
 


Loader.
Live Chat Icon For mobile
Up arrow icon