Introduction
This article shows how to use the dropdownlist helper in MVC applications.
- Index: wrapping data coming from Entity Framework (EF).
- Index1: wrapping data coming from the Controller's Action Result.
- Index2: wrapping data inside the view.
Create an ASP.Net MVC 4 Web Application

Figure 1 Web Application
Choose Internet Application

Figure 2 Internet Application
Add an EmployeeController

Figure 3 Add EmployeeController
Set up an Entity Framework

Figure 4 Entity Framework

Figure 5 Data Connection
Figure 6 Data Connection Object
Employeecontroller.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using DropDownListApp_MVC.Models;
- namespace DropDownListApp_MVC.Controllers
- {
- public class EmployeeController : Controller
- {
- //
- // GET: /Employee/
- EmployeeEntities objEmployeeEntities = new EmployeeEntities();
- public ActionResult Index()
- {
- ViewBag.List = new SelectList(objEmployeeEntities.Departments.Select(r => r.DepartmentName));
- return View();
- }
- public ActionResult Index1()
- {
- List<SelectListItem> items = new List<SelectListItem>();
- items.Add(new SelectListItem { Text = "IT", Value = "0" });
- items.Add(new SelectListItem { Text = "HR", Value = "1" });
- items.Add(new SelectListItem { Text = "Management", Value = "2" });
- ViewBag.List = items;
- return View();
- }
- public ActionResult Index2()
- {
- return View();
- }
- }
- }




Vijay SPosted May 26, 2015, 5:08 AM
Good
Santhakumar MunuswamyPosted May 25, 2015, 2:50 PM
Thanks for nice one
Gowtham RajamanickamPosted May 25, 2015, 5:08 AM
good