Asp net MVC Dropdownlist from database

July 02 2019 by Admin

In this article, we have described how to bind Dropdownlist in MVC from the database with an example and a sample code.


In this article, we have described how to bind Dropdownlist in MVC from the database with an example and a sample code.

We have used the razor view engine and binded the Dropdownlist from the database using ADO.net. It's a very simple way for Dropdownlist binding. In this example, we have taken employees and customers data for binding a Dropdownlist. You can see the below example code .

Required Namespaces

We will need to use the following namespaces.

using Dropddownlist_Project.Models;

using System.Configuration;

using System.Data.SqlClient;

Views

We have provided razor view html below.

@model Dropddownlist_Project.Models.Dropdownlist

@{

Layout = null;

}

How to bind mutiple Dropdownlist in mvc using C# Example

Customer Name:

@Html.DropDownListFor(model => model.custlist, new SelectList(Model.custlist, "custId", "custName"), new { @class = "form-control" })


Employee Name :

@Html.DropDownListFor(model => model.emplist, new SelectList(Model.emplist, "Empid", "EmpName"), new { @class = "form-control" })

Model

In this model we have two child classes for dropdownlist binding. you can use your existing class for this activity.

public class Dropdownlist

{

public Listemplist { get; set; }

public Listcustlist { get; set; }

}

public class Employee_list

{

public int Empid { get; set; }

public string EmpName { get; set; }

}

public class Customer_list

{

public int custId { get; set; }

public string custName { get; set; }

}

Controller

We have created DropDownlist name of controller.

public class DropDownlistController : Controller

{

// GET: DropDownlist

public ActionResult DropDownControl()

{

Dropdownlist multi_Dropdownlist = new Dropdownlist

{

custlist = GetCustomerList(),

emplist = GetEmployeeList()

};

return View(multi_Dropdownlist);

}

public ListGetCustomerList()

{

var connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

SqlConnection con = new SqlConnection(connection);

SqlCommand cmd = new SqlCommand("Select customerId,FirstName as Name From Customers", con);

con.Open();

SqlDataReader idr = cmd.ExecuteReader();

Listcustomers = new List();

if (idr.HasRows)

{

while (idr.Read())

{

customers.Add(new Customer_list

{

custId = Convert.ToInt32(idr["customerId"]),

custName = Convert.ToString(idr["Name"]),

});

}

}

con.Close();

return customers;

}

public ListGetEmployeeList()

{

var connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

SqlConnection con = new SqlConnection(connection);

SqlCommand cmd = new SqlCommand("Select * From Employees", con);

con.Open();

SqlDataReader idr = cmd.ExecuteReader();

ListEmployees = new List();

if (idr.HasRows)

{

while (idr.Read())

{

Employees.Add(new Employee_list

{

Empid = Convert.ToInt32(idr["EmpoyeeId"]),

EmpName = Convert.ToString(idr["EmpName"]),

});

}

}

con.Close();

return Employees;

}

}

Screenshot

Download



Note: All contents are copyright of their authors.