Step1: Create new project.
Step 2. Create controller ‘PopulateUsing_Code’ in controller folder.
Note:
SelectList create a list of items where user can select one item out of them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace PopulateDropDown.Controllers
{
public class PopulateUsing_CodeController : Controller
{
//
// GET: /PopulateUsing_Code/
public ActionResult Index()
{
return View();
}
}
}
Step 3: Add View in Action Section
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width” />
<title>Index</title>
</head>
<body>
<div>
//create HTML Form using Html.BeginForm htmlhelpers.
@using (Html.BeginForm())
{
//Below code is example of collection
@Html.DropDownList(“dd_state”, new List<SelectListItem>()
{
new SelectListItem{ Text=”Rajasthan”, Value=”1″},
new SelectListItem{Text=”UP”, Value=”2″},
new SelectListItem{Text=”MP” , Value=”3″}
}, “–Select State–“);
}
</div>
</body>
</html>
Output