Add Table Names In Dropdown List With Entity Framework In ASP.NET MVC

I am explaining in this article how to add the table name in the dropdown list from the database in MVC. Sometimes, we see the example in real time when we select the table name and display the respective results in the Gridview or the table etc. We will see step by step.

Step 1: We can see the number of tables in my database. My database name is System test.


Step 2: Now, we have to create an MVC Application to perform the task.

Open Visual Studio and take a new project.

Choose ASP.NET Web Application, give the name and click OK.

ASP.NET Web Application

Now, we choose MVC option and click OK.


Step 3: Now, we have to add the model class. For this, we right click on MVC project and add ADO .NET Entity Data Model. Right click MVC project on Solution Explorer, select add option and proceed, as shown below:


Add EF Designer from the database and click Next button.

EF Designer

Add clicking new connection option, give the Server name, select the database, click Test Connection and click OK.

new connection

Click next and select the tables which you want. Here, I selected all the tables and clicked OK.


Click Finish.

Step 4: Now, we have to add the controller. Right click on the controller folder, as shown below:


Select 'Controller Empty' option and Click Add button.


Now we have to write the codes in the controller class.

First, we have to add a namespace, shown below:

Using System.Reflection;

Now, write the full codes, given below:

  1. using System.Collections.Generic;  
  2. using System.Reflection;  
  3. using System.Web.Mvc;  
  5. namespace TablesWithDropdownExa.Controllers  
  6. {  
  7.     public class MyControllerController : Controller  
  8.     {  
  9.         // GET: MyController  
  10.         public ActionResult Index()  
  11.         {  
  12.             using (var Mydb = new SystemTestEntities())  
  13.             {  
  14.                 FieldInfo[] mytables = Mydb.GetType().GetFields(BindingFlags.Public |  
  15.                                        BindingFlags.NonPublic |  
  16.                                        BindingFlags.Instance);  
  18.                 List<string> tbllistname = new List<string>();  
  19.                 foreach (var item in mytables)  
  20.                 {  
  21.                     tbllistname.Add(item.Name.Split('<''>')[1]);  
  22.                 }  
  23.                 ViewBag.MyTables = tbllistname;  
  24.             }  
  25.                 return View();  
  26.         }  
  27.     }  
  28. }  
Now, we have to add the view. For this, right click the index method, add the view and write the codes in Index.chtml page.
  1.     ViewBag.Title = "Index";  
  2. }  
  4. <h2 class="text-info">Welcome my dropdown list example</h2>  
  5. <table>  
  7.     <tr>  
  9.         <td>  
  11.             @Html.DropDownList("tables", new SelectList(ViewBag.MyTables), new { id = "ddl1" })  
  13.         </td>  
  15.     </tr>  
  16. </table>  
And finally see the result,
I hope you enjoyed this. Thank you for looking at the example.

Similar Articles