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.
Unfortunately, activation email could not send to your email. Please try again.

How to Connect DropDownList to SQL DataSource?

Thread ID:





127440 Nov 15,2016 11:02 PM Nov 17,2016 06:55 AM ASP.NET Core 1
Tags: DropDownList
Sid Childers
Asked On November 15, 2016 11:02 PM

I'm using a database first approach and have a lookup table that needs to be used to populate a DropDownList in my ASP.NET Core application.  I could not find documentation or code samples that shows how to wire up the model's entity class to the DropDownList.  Is this feature available for ASP.NET Core?  Or, can I mix in the ASP.NET MVC DropdownList which has viable documentation?  

Thanks for your help and guidance.

Prince Oliver [Syncfusion]
Replied On November 17, 2016 06:55 AM

Hi Sid,   
Thanks for contacting Syncfusion support.   
Query 1: How to Connect DropDownList to SQL DataSource? Is this feature available for ASP.NET Core?  Or, can I mix in the ASP.NET MVC DropdownList which has viable documentation?  
Yes, it is available in ASP.NET Core. There is no need to use MVC Dropdownlist in Core Project as Core Dropdownlist supports all the features available MVC Dropdownlist.   
In order to connect a SQL data source to dropdownlist, you have to create a list and add the required data from the data table to the list using SQL command and return the list to the view page. Have a look at the below code snippet.     
Controller page   
List<Orders> data = new List<Orders>();   
        public IActionResult Index()   
            string RootPath = _hostingEnvironment.ContentRootPath;   
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + RootPath +"\\Data\\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30");   
            using (con)   
                SqlDataReader myReader = null;   
                SqlCommand myCommand = new SqlCommand("select * From [Orders]",   
                myReader = myCommand.ExecuteReader();   
                while  (myReader.Read())   
                    data.Add(new Orders(myReader["ShipName"].ToString()));   
                ViewBag.datasource = data;   
            return View();   
In the view page, bind ViewBag list to the dropdownlist’s datasource API. Have a look at the following code snippet.   
View Page   
<ej-drop-down-list id="bikeList"  datasource="(IEnumerable<Orders>)ViewBag.datasource" watermark-text="Select ShipName"width="100%">   
            <e-drop-down-list-fields  text="ShipName"  />   
We have included a sample for your convenience. Please refer to the Sample link.   


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.