To populate the DropDownList in MVC using Razor, we can use multiple ways like ViewBag, ViewData, Tempdata, jQuery, Model, Database, AJAX, and Hardcoding in View.
DropdownList using Hardcoded data in view
Populating With Hardcoded Data
@Html.DropDownList("MySkills", new List<% foreach=""><%: string.format=""><%: item.name=""><%: html.actionlink=""><%: html.actionlink=""><%: htmlactionlink=""><%}%>
{
new SelectListItem{ Text="ASP.NET MVC", Value = "1" },
new SelectListItem{ Text="ASP.NET WEB API", Value = "2" },
new SelectListItem{ Text="ENTITY FRAMEWORK", Value = "3" },
new SelectListItem{ Text="DOCUSIGN", Value = "4" },
new SelectListItem{ Text="ORCHARD CMS", Value = "5" },
new SelectListItem{ Text="JQUERY", Value = "6" },
new SelectListItem{ Text="ZENDESK", Value = "7" },
new SelectListItem{ Text="LINQ", Value = "8" },
new SelectListItem{ Text="C#", Value = "9" },
new SelectListItem{ Text="GOOGLE ANALYTICS", Value = "10" },
})
DropdownList using Viewbag
public ActionResult Index() {
#region ViewBag
List < SelectListItem > mySkills = new List < SelectListItem > () {
new SelectListItem {
Text = "ASP.NET MVC", Value = "1"
},
new SelectListItem {
Text = "ASP.NET WEB API", Value = "2"
},
new SelectListItem {
Text = "ENTITY FRAMEWORK", Value = "3"
},
new SelectListItem {
Text = "DOCUSIGN", Value = "4"
},
new SelectListItem {
Text = "ORCHARD CMS", Value = "5"
},
new SelectListItem {
Text = "JQUERY", Value = "6"
},
new SelectListItem {
Text = "ZENDESK", Value = "7"
},
new SelectListItem {
Text = "LINQ", Value = "8"
},
new SelectListItem {
Text = "C#", Value = "9"
},
new SelectListItem {
Text = "GOOGLE ANALYTICS", Value = "10"
},
};
ViewBag.MySkills = mySkills;
#endregion
return View();
}
Populating With ViewBag Data
@Html.DropDownList("MySkills", (IEnumerable
<% foreach=""><%: string.format=""><%: item.name=""><%: html.actionlink=""><%: html.actionlink=""><%: html.actionlink=""><%}%>)ViewBag.MySkills)
DropdownListusing ViewData
public ActionResult Index() {
#region ViewData
List < SelectListItem > mySkills = new List < SelectListItem > () {
new SelectListItem {
Text = "ASP.NET MVC", Value = "1"
},
new SelectListItem {
Text = "ASP.NET WEB API", Value = "2"
},
new SelectListItem {
Text = "ENTITY FRAMEWORK", Value = "3"
},
new SelectListItem {
Text = "DOCUSIGN", Value = "4"
},
new SelectListItem {
Text = "ORCHARD CMS", Value = "5"
},
new SelectListItem {
Text = "JQUERY", Value = "6"
},
new SelectListItem {
Text = "ZENDESK", Value = "7"
},
new SelectListItem {
Text = "LINQ", Value = "8"
},
new SelectListItem {
Text = "C#", Value = "9"
},
new SelectListItem {
Text = "GOOGLE ANALYTICS", Value = "10"
},
};
ViewData["MySkills"] = mySkills;
#endregion
}
Populating With ViewData Data
@Html.DropDownList("MySkills", (IEnumerable
<% foreach=""><%: string.format=""><%: item.name=""><%: html.actionlink=""><%: html.actionlink=""><%: html.actionlink=""><%}%>)ViewData["MySkills"])
DropdownList using TempData
#region TempData
List < SelectListItem > mySkills = new List < SelectListItem > () {
new SelectListItem {
Text = "ASP.NET MVC", Value = "1"
},
new SelectListItem {
Text = "ASP.NET WEB API", Value = "2"
},
new SelectListItem {
Text = "ENTITY FRAMEWORK", Value = "3"
},
new SelectListItem {
Text = "DOCUSIGN", Value = "4"
},
new SelectListItem {
Text = "ORCHARD CMS", Value = "5"
},
new SelectListItem {
Text = "JQUERY", Value = "6"
},
new SelectListItem {
Text = "ZENDESK", Value = "7"
},
new SelectListItem {
Text = "LINQ", Value = "8"
},
new SelectListItem {
Text = "C#", Value = "9"
},
new SelectListItem {
Text = "GOOGLE ANALYTICS", Value = "10"
},
};
TempData["MySkills"] = mySkills;
#endregion
Populating With TempData Data
@Html.DropDownList("MySkills", (IEnumerable
<% foreach=""><%: string.format=""><%: item.name=""><%: html.actionlink=""><%: html.actionlink=""><%: html.actionlink=""><%}%>)TempData["MySkills"])