TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Add DropDownList Value from Database in ASP.NET MVC
Jagdeep Singh Jhass
May 03
2016
Code
1.4
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
First add model customer
public
partial
class
customer
{
public
int
Id {
get
;
set
; }
[Required]
[StringLength(50)]
public
string
Name {
get
;
set
; }
[Required]
[StringLength(50)]
public
string
City {
get
;
set
; }
}
Add controller CustomerController
private
ModelCustomer db =
new
ModelCustomer();
public
ActionResult Index()
{
var q = db.customers.Select(c =>
new
SelectListItem { Value = c.City, Text = c.City });
ViewBag.City = q.ToList();
return
View();
}
Now Add View:
@model WebApplication1.Models.customer
@{
Layout =
null
;
}
<!DOCTYPE html>
<html>
<head>
<meta name=
"viewport"
content=
"width=device-width"
/>
<title>Index</title>
</head>
<body>
<div>
@Html.DropDownList(
"City"
,ViewBag.City
as
SelectList,
"Select City"
)
</div>
</body>
</html>
Now build project and values of City column of Customer table can be seen in dropdownlist.
Add DropDownList value
ASP.NET
database
MVC