Default value for Autocomplete

Dear,

How can I set the default value to "BE" (Belgium) in the following AutoComplete?

<ejs-autocomplete id="CountryCodeISOAlpha2" dataSource="@ViewBag.Countries" placeholder="" filtering="filteringCountry" autofill="true" itemTemplate="<span class='item'><span class='name'>${Name}</span> (<span class='code'>${Code}</span>)</span>">
<e-autocomplete-fields text="Name" value="Code"></e-autocomplete-fields>
</ejs-autocomplete>


Thanks,
Koen

5 Replies

PM Ponmani Murugaiyan Syncfusion Team April 9, 2020 06:01 AM UTC

Hi Koen, 
 
Greetings from Syncfusion support. 
 
We have validated you reported query. Autocomplete component supports value field alone. We have prepared sample for set default value to autocomplete component. Please find the code snippet and test sample below for reference. 
 
[Index.cshtml] 
 
<ejs-autocomplete id="country" dataSource="@ViewBag.data" value="@ViewBag.value" placeholder="e.g. india" popupheight="220px" itemTemplate="@Html.Raw("<span><span class='name'>${Name}</span><span class ='code'>${Code}</span></span>")"> 
    <e-autocomplete-fields value="Name"></e-autocomplete-fields> 
</ejs-autocomplete> 
 
 
[HomeController.cs] 
 
public class HomeController : Controller 
        { 
 
        public IActionResult Index() 
            { 
            ViewBag.data = new Countries().CountriesList(); 
            ViewBag.value = "Bermuda"; 
            return View(); 
            } 
        } 
 
 
 
Regards, 
Ponmani M 



KO Koen replied to Ponmani Murugaiyan April 9, 2020 02:52 PM UTC

Hi Koen, 
 
Greetings from Syncfusion support. 
 
We have validated you reported query. Autocomplete component supports value field alone. We have prepared sample for set default value to autocomplete component. Please find the code snippet and test sample below for reference. 
 
[Index.cshtml] 
 
<ejs-autocomplete id="country" dataSource="@ViewBag.data" value="@ViewBag.value" placeholder="e.g. india" popupheight="220px" itemTemplate="@Html.Raw("<span><span class='name'>${Name}</span><span class ='code'>${Code}</span></span>")"> 
    <e-autocomplete-fields value="Name"></e-autocomplete-fields> 
</ejs-autocomplete> 
 
 
[HomeController.cs] 
 
public class HomeController : Controller 
        { 
 
        public IActionResult Index() 
            { 
            ViewBag.data = new Countries().CountriesList(); 
            ViewBag.value = "Bermuda"; 
            return View(); 
            } 
        } 
 
 
 
Regards, 
Ponmani M 


Dear,

you say "Autocomplete component supports value field alone", however the examples at https://ej2.syncfusion.com/aspnetcore/documentation/auto-complete/data-binding/?no-cache=1#array-of-complex-object show both value and text fields...?

secondly, you propose to set the default value with "value="@ViewBag.value" " . Is it not possible to set it with "value="BE" " ?

Thanks,

Koen




JM Jeyanth Muthu Pratheeban Sankara Subramanian Syncfusion Team April 10, 2020 07:13 AM UTC

Hi Koen, 

Thanks for your update.

 
We have validated your query regarding  “Autocomplete supports both text and value fields” in the  documentation. We regret for the inconvenience caused and we will correct and refresh the documentation and for setting default value for the component we would suggest you to set value in the created event as like below which triggers when the control is created where you can assign the value directly. 


 
<ejs-autocomplete id="country" dataSource="@ViewBag.data" placeholder="e.g. india" popupheight="220px" created="onCreate" itemTemplate="@Html.Raw("<span><span class='name'>${Name}</span><span class ='code'>${Code}</span></span>")"> 
    <e-autocomplete-fields value="Name"></e-autocomplete-fields> 
</ejs-autocomplete> 
 
<script> 
    function onCreate() { 
        this.value = "Bermuda" 
    } 
</script> 
 

We have update the sample. Please find the sample below and get back to us if you need any further assist on this.

Sample Link : https://www.syncfusion.com/downloads/support/forum/153124/ze/EJ2_Core_Autocomplet_defultvalue728389381 

Regards, 
Jeyanth. 



CK chris kraft July 25, 2021 12:06 PM UTC

I am concerned about this product since this post was submitted over a year ago and stated the documentation would be updated.  I find the documentation is still out of date and for the auto complete field it still shows it can accept a text attribute. 

In addition, I have found that using a ViewBag doesn't work in my case.  Possibly because I am using an item template with two columns of data.

The option to set it in the OnCreate is not something I prefer to utilize since I'm setting all my defaults in the controller and am basing it on calculations.  While I realize the foundation of the product is largely javascript, it does still need to support code behind options.

I am posting this in the hopes that this matter will be reviewed, the documentation updated, and a solution that supports asp .net core is made available to us i.e. not using javascript on the form.



DR Deepak Ramakrishnan Syncfusion Team July 26, 2021 11:54 AM UTC

Hi Chris 
 
Greetings from Syncfusion support. 
 
We have checked the reported issues from our end and found that we have updated the documentation already , please find the below documentation link for your reference. And  we have created a sample based on your requirement.Also please update your nuget version to the latest(19.2.0.48) and check if the issue get reproduced  . If this does not meet your requirement , kindly share the below details to validate the issue further . 
 
  1. Simple sample which reproduces the issue.
  2. Video demonstration which reproduces the issue.
 
 
 
[index.cshtml] 
 
<ejs-autocomplete id="employees" dataSource="@ViewBag.data" value="@ViewBag.Value" placeholder="e.g. Andrew Fuller" popupHeight="450px" 
   headerTemplate="@Html.Raw("<div class=\"header\"> <span class=\"info\">Employee Name</span><span    class=\"info\">Designation</span> </div>)")"
     itemTemplate="@Html.Raw("<div><div
class=\"ename\"> ${FirstName} - ${Designation}</div></div>")"> 
      <e-autocomplete-fields value="FirstName"></e-autocomplete-fields> 
</ejs-autocomplete> 
 
 
Kindly revert us, if you have any concerns.  
 
Regards,  
Deepak R 


Loader.
Up arrow icon