How to load Dropdownlist from database in MVC or Populate Dropdownlist in MVC from database

Introduction:

In this article I am going to explain an easy way to populate dropdownlist in MVC.  There are lots of other options to populate dropdownlist in MVC, However here I am going to explain two options which I have come across.

Solution:

Model:

First you need to create a class in the Model. In this example I have used the DatabaseFirst approach.

You can use the below steps to generate the class files

  • Right click on the Model folder
  • Select Add option
  • And then select the New Item option
  • Select the Net Entity Data Model option and Provide a name for it
  • In the Entity Data Model Wizard, selectEF Designer from database and click on Next

If you already have connection string configured then you can select that from the available list.

If you don’t have the connection string configured then use the New Connection option and create the connection

  • Expand the Table option and then select the tables which you want
  • Click on Finish
  • You will have necessary class files generated in your solution now

Untitled

You can get more details about how to use DatabaseFirst approach from this link.

Controller:

Once the needed model files are generated now we need to create the code to get the data from database and then pass it to view for dropdownlist.

You can use the below code to get the value from database and pass it to ViewBag

 public ViewResult Index()
         {
             //Create db context object here 
             AdventureWorksDbContext dbContext = new AdventureWorksDbContext();
             //Get the value from database and then set it to ViewBag to pass it View
             IEnumerable<SelectListItem> items = dbContext.Employees.Select(c => new SelectListItem
                   {
                       Value = c.JobTitle,
                       Text = c.JobTitle
 
                   });
             ViewBag.JobTitle = items;
             return View();
         }
 

View:

Now you need to assign the values in ViewBag to dropdownlist like given below

@Html.DropDownList("JobTitle", "Select a Value")

You will have the dropdownlist populated with data now

, , , , ,

  1. #1 by shobhit on March 16, 2016 - 9:55 am

    good… Thanks

    Like

  2. #2 by Amit Kumar on May 27, 2016 - 10:21 am

    thank u

    Like

  3. #3 by srinivas g on September 16, 2016 - 7:03 am

    AdventureWorksDbContext dbContext = new AdventureWorksDbContext();
    where this class is created

    Like

    • #4 by A2H on September 20, 2016 - 5:04 pm

      I have used the option provided by Entity Framework to map existing database. You can refer the 3rd option in this link to create dbcontext classes

      Like

  4. #5 by Jae Rakes on December 13, 2016 - 8:33 am

    Rattling good info can be found on weblog . “I believe in nothing, everything is sacred. I believe in everything, nothing is sacred.” by Tom Robbins.

    Like

  5. #6 by chafurbate on January 16, 2017 - 11:18 pm

    I do not even understand how I ended up right here, however I believed this put up was once great.
    I do not realize who you might be however definitely you’re going to a famous blogger in case you are
    not already. Cheers!

    Like

  6. #7 by SoftPower on May 19, 2018 - 7:00 am

    Code below worked for me thanks!

    HicAccountEntities dbContext = new HicAccountEntities();
    IEnumerable items = dbContext.tbBanks.Select(c => new SelectListItem
    {
    Value = c.bID,
    Text = c.bName + ” ” + c.bAccountNumber
    });
    ViewBag.MyBanks = items;
    return View();

    Like

Leave a comment