How to Populate the DropDownList in ASP.NET MVC Razor View?

The DropDownList is one of the most useful controls that can be used in various scenarios that involves selecting the data from the list.

The ASP.NET MVC Framework provides the Html.DropDownList and Html.DropDownListFor helper methods which can be used to create DropDownList.

How to Populate DropDownList in ASP.NET MVC Razor View?

Below is a sample source code demonstrating how to Populate DropDownList in ASP.NET MVC Razor View.

View

@{

if (@ViewContext.ViewData.ModelState.IsValid)

{

ViewBag.Title = "Index";

}

}

<h2>Abundant Code</h2>

@Html.DropDownList(“StateType”)

Controller

using MvcApplication1.Models;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace MvcApplication1.Controllers

{

public class EmployeeController : Controller

{

public ActionResult Index()

{

ViewBag.ApplicationTitle = "AbundantCode App";

List<SelectListItem> obj = new List<SelectListItem>();

obj.Add(new SelectListItem { Text = "Solid State", Value = "1" });

obj.Add(new SelectListItem { Text = "Liquid State", Value = "2" });

obj.Add(new SelectListItem { Text = "Gas State", Value = "3" });

ViewBag.StateType = obj;

return View();

}

}

}
How to Populate the DropDownList in ASP.NET MVC Razor View?