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

Key Value to the controller from Autocomplete box

Is it possible to send the key value from the autocomplete box to the controller instead of the text value on Submit command?
Or is there a way to achieve this with a Dropdownlist in combination with the autocomplete function?

4 Replies

ES Ezhil S Syncfusion Team August 25, 2014 06:39 AM UTC

Hi Tom, 

Thank you for your interest with Syncfusion products. 

Yes, it is possible to send the Key value of the selected item in Autocomplete textbox to controller instead of the Text value. However we haven’t provided default property for sending the Key Value to controller, so you have to get the Key value of the Selected item using OnValueChange event and then pass the value to controller using AJAX post. Define the client side script for OnValueChange event, to fetch the Key value from the Suggestion list and store it to a variable. On Button click event, using AJAX post pass the variable that holds the Key value. Please refer the following code snippet and attached sample.

<code>

[CSHTML]

@Html.Syncfusion().AutocompleteTextBox("Autocomplete").RequestMapper("GetData").DropDown(true).Width(160).ClientSideValueChange("onValueChange")

                    <input type="button" value="Submit" name="button" onclick="BtnClick(this)

[Script]

<script type="text/javascript">

    var acvalue;

    function onValueChange(sender, args) {

        if (args._value == "")

            args._value = "Empty";

        for(var i=0;i<sender._suggestion.length;i++){

            if (args._dataTextItem == sender._suggestion[i].Text)

                acvalue = sender._suggestion[i].Value;

        }       

    }

    function BtnClick(args) {

        $.ajax({

            type: 'POST',

            url: '/Tools/ToolsFeatures',

            data: { val: acvalue },

            success: function (data) {               

            }

        });

    }

</script>

[Controller]

//Post action on button click

[AcceptVerbs(HttpVerbs.Post)]

    public ActionResult ToolsFeatures(string val)

    {

        var datavalue = val;

        return View();

    }

//Request mapper for Autocomplete

[AcceptVerbs(HttpVerbs.Post)]

        public ActionResult GetData(string QueryString)

        {

            List<string> data = new List<string>();

            List<ACsugg> acdata = new List<ACsugg>();

            acdata.Add(new ACsugg("List1", "JQuery Vs Java Script"));

            acdata.Add(new ACsugg("List2", "JQuery"));

            acdata.Add(new ACsugg("List3", "Pro Sync Framework"));

            acdata.Add(new ACsugg("List4", "Asp.net MVC Tutorial"));

            acdata.Add(new ACsugg("List5", "Professional ASP.NET MVC 4 "));

            acdata.Add(new ACsugg("List6", "Windows 8 Apps with XAML "));

            acdata.Add(new ACsugg("List7", "Syncfusion jQuery_Succinctly"));

            acdata.Add(new ACsugg("List8", "Android Application Development"));

            acdata.Add(new ACsugg("List9", "Basics in C and C++"));

            acdata.Add(new ACsugg("List10", "Dot net Application development"));

            acdata.Add(new ACsugg("List11", "Programming in C#"));

            acdata.Add(new ACsugg("List12", "Advanced Java"));

            var datasource = acdata.AsQueryable();           

            return datasource.AutocompleteActionResult();

        }

</code> 

Kindly let us know if you have further queries. 

Regards,

Ezhil S


Attachment: Autocomplete_f117149_cc2f1ec6.zip


AL Andy Lim April 14, 2019 08:28 AM UTC

How to get it (key value) from code behind? Thanks.



AB Ashokkumar Balasubramanian Syncfusion Team April 15, 2019 09:53 AM UTC

Hi Andy Lim,  
 
Yes, you can get the selected key value in code behind through server-side event “OnValueSelect”. Please refer to the below given code 
 
<ej:Autocomplete ID="multicolumn" runat="server" Width="500px" WatermarkText="Select a order" ShowPopupButton="true" DataTextField="CustomerID" DataUniqueKeyField="OrderID" OnValueSelect="multicolumn_ValueSelect"> 
</ej:Autocomplete> 
 
protected void multicolumn_ValueSelect(object sender, Syncfusion.JavaScript.Web.AutocompleteSelectEventArgs e) 
{ 
            var val = e.Key; 
} 
 
Now you will receive the event arguments in code behind as shown below 
 
 
 
We have attached a sample for reference which can be downloaded from the below link 
 
 
Please let us know, if you have any concern on this. 
 
Regards, 
Ashokkumar B. 



MI mistakiesh August 10, 2021 02:50 PM UTC

Thanks a ton for this helpful resource, been into this key-store for a moment and eventually bumped into the PHP redis extension, where Redis is a server that provides a key/value store. It might complete my homework for blogs. Thanks! :) 


Loader.
Live Chat Icon For mobile
Up arrow icon