ASP.NET MVC dropdown-list from database

how to bind dropdownlist in mvc using viewbag from database
how to bind dropdownlist in mvc 5 from database using entity framework
how to bind dropdownlist in mvc from database without using entity framework
mvc populate dropdownlist from database
populate html dropdownlist from database
dynamically populate dependent dropdown list in mvc
how to bind dropdownlist in mvc 4 razor from database mvc dropdownlist

Ok, so I'm new to this whole MVC-world, but it seems to be a pretty good way of getting things done and I'm trying to make it work here.

The problem is: I can't get data from my table in my SQL-database to a simple drop-down form on my registration page.

I have just no idea where to put the stuff, where to code to open the table, select the ids, where to put the response.write and how do I send it to the view?

My Model is this:

    public class users
    public string name {get; set;}
    public int user_id {get; set;}

My Controller is this:

    public ActionResult ListUser()
        return View();

And my View is this:

@model Community.Models.users

I have googled for 2 days now and watched several videos on youtube but of no use, I can't find it. Please, anyone with some knowledge here? And please point me to some good tutorials and/or forums where I can browse for more questions I might have

Still no luck on this project..

I'm creating a form and within that form, i want a db-loop (IEnumerable).. But the current model is not a IEnumerable. I'm pretty much stuck, watched a bunch of tutorials and they all just list ONE connection, what if I want two models?

Here is my Controller, I get that you must pass a list to the view, right?

    public ActionResult Registration()
        return View(db.users.ToList());

How do i get hold of that list in my view witout an IEnumerable model?

@neoistheone, your example didnt help me much, my DB opens like this:

private DataBaseContext db = new DataBaseContext();

and i don't know how, but it opens the connection. I've tried for so many hours now, its just silly, haven't slept for soo long!

I'm used to programming ASP-Classic fyi, and this is my first serious try to upgrade my knowledge about programing an up-to-date language and OOP.

Add the SelectList to your model:

public SelectList DropDownList { get; set; }

build the class for that collection:

public class MyListTable
    public string Key { get; set; }
    public string Display { get; set; }

and then in your controller, load the data for the MyListTable class from the database:

var list = new List<MyListTable>();

using (SqlConnection c = new SqlConnection(cString))
using (SqlCommand cmd = new SqlCommand("SELECT KeyField, DisplayField FROM Table", c))
    using (SqlDataReader rdr = cmd.ExecuteReader())
        while (rdr.Read())
            list.Add(new MyListTable
                Key = rdr.GetString(0),
                Display = rdr.GetString(1)

var model = new users();
model.DropDownList = new SelectList(list, "Key", "Display");

and then finally, you need to send your model to the view:

return View(model);

Now in the Razor you can display this:

@Html.DropDownListFor(m => Model.DropDownList);

You of course can name these things better names, but you get the idea.

ASP.NET MVC dropdown-list from database, Add the SelectList to your model: public SelectList DropDownList { get; set; }. build the class for that collection: public class MyListTable { public string Key { get ;� The above code is to retrieve data from SQL Server database and bind the data to dropdown list control in ASP.NET Core MVC application. The next step is to add a view page for the create action method defined inside the controller class. To add a view page, select the respective action method, right click and click on add view option at the top.

There a great answers already but Here is another approach.

You will use user as a model, ListUserViewModel as view-model and UserController as the contoller. The work of view-model is to carry all info needed to be displayed on the page from the controller without adding unwanted properties into the model class. In your case list of users from database into the drop down list.


public class User     //By the way use singular when naming a class
    public string name {get; set;}
    public int user_id {get; set;}


public class ListUserViewModel
    public list<User> Users{get; set;}


public class UserController : Controller
    private DataBaseContext db = new DataBaseContext();

    public ActionResult ListUser()
        var users = db.Users.ToList();

        var viewModel = new ListUserViewModel { Users = users };

        return View(viewModel);

Now use ListUserViewModel instead of User in your view as a model

@model Community.Models.ListUserViewModel

and the drop down

@Html.DropDownListFor(m => m.Users, new SelectList(Model.Users, "user_id", "name"), " ")


You are creating drop down list for Users with Model.Users as select list data source. "user_id" as a value of the selected user and "name" as display label. the last argument( i put empty string " ") is a default value that the drop down will display before selection.

I hope this will help you or someone else.

Binding Dropdownlist With Database In MVC, There are server controls in ASP.NET webforms that are easy to bind. And in the same way in an Edit Form this shows how to dropdownlist� This article provides an explanation about how to populate a dropdown list from the database using stored procedure in Asp.Net MVC with example and also show you how you can get selected value from the dropdown list and use of @Html.Dropdownlist or @Html.DropdownlistFor properties to bind dropdown list based on the values from the database.

Try this,


public string CoutryID { get; set; }
public List<SelectListItem> CountryList { get; set; }

Controller method which fill the list

public List<Country> getCountryList()
            using (QRMG_VendorPortalDataContext _context = new QRMG_VendorPortalDataContext())
                return (from c in _context.Countries
                        where c.IsDeleted == false
                        select c).ToList();

Drop down list in View

 @Html.DropDownListFor(m => m.CoutryID,
         new SelectList(Model.CountryList,
                        "CoutryID", "Value"))

Populate Dropdown Lists in ASP.NET MVC Razor Views, ASP.NET MVC - Populating Dropdown Lists in Razor Views Using the between the SQL Server database and the data entities in the model. Populate ASP.NET MVC Dropdownlist from Database. In this post we will see How we can Bind or Populate ASP.NET MVC Dropdownlist from Database (MS SQL Database). MVC Dropdownlist will be populated using MVC Model class Countries.cs and @Html.DropDownListFor html design.

If you are really new to ASP.Net MVC, this is a quite good Tutorial that shows you how the MVC-Pattern works.


Here is the sample-code to download:

this is an helpful video:

Populate ASP.NET MVC Dropdownlist from Database • ParallelCodes, In this post we will see How we can Bind or Populate ASP.NET MVC Dropdownlist from Duration: 1:29 Posted: Nov 26, 2018 Now I will explain how to bind or populate dropdownlist from database in mvc with example. In mvc we can implement dropdownlist by using two properties either @Html.DropDownList model or @Html.DropDownListFor model. Before we start implementing first design userdetails table in database and insert some data like as shown below.

I find this system works (and avoids using ViewBag):

View Model:

public class YourViewModel
    // This could be string, int or Guid depending on what you need as the value
    public int YourDropdownSelectedValue { get; set; }
    public IEnumerable<SelectListItem> YourDropdownList { get; set; }


// Get database values (by whatever selection method is appropriate)
var dbValues = db.YourEntity.ToList();

// Make Selectlist, which is IEnumerable<SelectListItem>
var yourDropdownList = new SelectList(dbValues.Select(item => new SelectListItem
    Text = item.YourSelectedDbText,
    Value = item.YourSelectedDbValue
}).ToList(), "Value", "Text");

// Assign the Selectlist to the View Model   
var viewModel = new YourViewModel(){
    // Optional: if you want a pre-selected value - remove this for no pre-selected value
    YourDropdownSelectedValue = dbValues.FirstOrDefault(),
    // The Dropdownlist values
    YourDropdownList = yourDropdownList

// return View with View Model
return View(viewModel);

and in the View:

@Html.DropDownListFor(a => a.YourDropdownSelectedValue, Model.YourDropdownList, "select this text - change this to null to exclude", new { @class = "your-class" })

mvc dropdownlist create bind from database c#4.6 using viewbag , #viewbagdropdownlist mvc tutorials latest version creating binding data into dropdownlist Duration: 8:42 Posted: Jul 9, 2018 Developers coming from a background of ASP.NET MVC will find it easy to work with ASP.NET Core MVC. If you are new to .NET Core read these articles to kickstart your .NET Core knowledge: Creating a New .NET Core Web Application Using Visual Studio 2015; Creating A New .NET Core Web Application With Command Line Interface (CLI) Topic. Database part.

How To Create DropDownList In MVC With Example, The tutorial discusses how to create dropdownlist from SQL server database using of HTML Duration: 23:01 Posted: Feb 18, 2017 Simply Create Dropdown List From Database In MVC 5.0. Introduction. In this article, we are learning how to create dropdown with the help of Entity Framework from database.

Bind (Populate) DropDownList from database using Model in ASP , In this article I will explain with an example, how to bind (populate) DropDownList from database using Model in ASP.Net MVC Razor. Partial Views on mvc create view that use a dropdown list to populate the partial view's view bag is this possible in mvc? 0 ASP.NET CORE: Passing result of stored procedure to dropdown

How to load Dropdownlist from database in MVC or , How to load Dropdownlist from database in MVC or Populate Dropdownlist in MVC from database. Introduction: In this article I am going to� The HTML page renders a select element similar to the simple ASP.NET MVC test page. Right click the browser window and select view source. The HTML markup for the select list is essentially identical. Test the HTML page, it works like the ASP.NET MVC action method and view we previously tested. Improving the Movie Select List with Enums

  • Check here:… and here:…
  • Check
  • This looks good, only, my connection to the database looks like this: using (var db = new communityContext()), and I have NO idea how to "SELECT * FROM ......"
  • @Tobias, you don't have to connect to the database the same way I do. I'm just giving you an example. You're not going to be able to just copy and paste this solution, you'll have to fit it in of course, I'm not writing your software - just helping you with this issue - so there's no way the code will just fit in.
  • @Tobias, the same way you get all rows from any table using a DbContext. You access the property that's available for that table. Like I said, you need to fit this in. You might be able to get rid of the MyListTable class because you have a DbContext. You could send a List of those in to the SelectList class and use different key and display fields. Look up the API's and learn them, understand them at an abstract level.
  • I understand that, but the SQL-query you wrute.. How do i wrote it in my application, its not the way you did it. I guess I'm just stupid not to have figured it out yet.
  • +1 Great and detailed. I was trying everything to populate DDL and post selected value in Controller. All gone in vain until ur solution. Thanks man