Articles in this section

How to get the selected value of the AutoComplete?

How to get the selected value in postback in AutoCompleteFor Control


The AutoComplete control supports strongly-typed HTML helpers represented by lambda expressions that have model or template passed into the View. So, you can get the selected value from view in the Controller part.

To achieve this, create an AutocompleteFor control and bound the dataSource from controller part.

Refer to the following code for razor file.

[Index.cshtml]

<div class="row">
    @using (Html.BeginForm())
     {
        <div> Select Component/ s:</div>
 
        @Html.EJ().AutocompleteFor(model => model.selected, (Syncfusion.JavaScript.Models.AutocompleteProperties)ViewData["auto"]).AutocompleteFields(f => f.Text("text").Key("uniqueKey")).Width("100%")
 
        @Html.EJ().Button("btn").Size(ButtonSize.Small).Text("Post").Type(ButtonType.Submit)
 
     }
</div>

 

In a button click, you can post the selected value to controller part. To update the selected value in model, pass FormCollections as a parameter in Index method. Refer to the following code snippet.

 [HomeController.cs]

   [HttpPost]
        public ActionResult Index(Autocmplete model, FormCollection values)
        {
 
            AutocompleteProperties selected = new AutocompleteProperties();
            selected.DataSource = CarsList.GetCarList();
            ViewData["auto"] = selected;          
            model.selected = values["selected"];             
            return View(model);
 
        }     

 

 Refer to the following screenshot for selected value in the controller.

Selected Value in Postback

Figure 1: Selected Value in Postback

Refer to the following link for the sample.

https://www.syncfusion.com/downloads/support/directtrac/general/ze/Autocomplete-30063326.zip

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied