Here, GetSelectListItems methods totakes a list of skills and returns a list of SelectListItem objects
- private IEnumerable < SelectListItem > GetSelectListItems(IEnumerable < MySkill > elements) {
- var selectList = new List < SelectListItem > ();
- foreach(var element in elements) {
- selectList.Add(new SelectListItem {
- Value = element.ID.ToString(),
- Text = element.Name
- });
- }
- return selectList;
- }
And create a DropDownList in View
- @model VariousWayBindingDropDownListInMVC5.Models.MySkills
- <tr>
- <td> Populating With Model Data </td>
- <td> @Html.DropDownList("FromModel", Model.Skills) </td>
- </tr>
Now run your application
Populate a DropwonList using Global Static Data in View
Now just create the global static SelectListItem items in view and assign it to the DropDownList
- @ {
- List < SelectListItem > listItems = new List < SelectListItem > ();
- listItems.Add(new SelectListItem {
- Text = "ASP.NET MVC",
- Value = "1"
- });
- listItems.Add(new SelectListItem {
- Text = "ASP.NET WEB API",
- Value = "2",
- Selected = true
- });
- listItems.Add(new SelectListItem {
- Text = "DOCUSIGN",
- Value = "3"
- });
- listItems.Add(new SelectListItem {
- Text = "C#",
- Value = "4"
- });
- } < tr > < td > Populating With Global static Data < /td> < td > @Html.DropDownList("StaticData", listItems) < /td> < /tr>
Now run your application
Controller Controller (DropDownListController.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VariousWayBindingDropDownListInMVC5.Models;
namespace VariousWayBindingDropDownListInMVC5.Controllers
{
public class DropDownListController : Controller
{
//
// GET: /DropDownList/
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
#region ViewData
ViewData["MySkills"] = mySkills;
#endregion
#region TempData
TempData["MySkills"] = mySkills;
#endregion
#region Enum
var myskill = new List<ConvertEnum>();
foreach (MySkills lang in Enum.GetValues(typeof(MySkills)))
myskill.Add(new ConvertEnum { Value = (int)lang, Text = lang.ToString() });
ViewBag.MySkillEnum = myskill;
#endregion
#region Database with EF
using (CSharpCornerEntities cshparpEntity = new CSharpCornerEntities())
{
var fromDatabaseEF = new SelectList(cshparpEntity.MySkills.ToList(), "ID", "Name");
ViewData["DBMySkills"] = fromDatabaseEF;
}
#endregion
#region Model
var model = new VariousWayBindingDropDownListInMVC5.Models.MySkills();
using (CSharpCornerEntities cshparpEntity = new CSharpCornerEntities())
{
var dbData = cshparpEntity.MySkills.ToList();
model.Skills = GetSelectListItems(dbData);
}
#endregion
return View(model);
}
private IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<MySkill> elements)
{
var selectList = new List<SelectListItem>();
foreach (var element in elements)
{
selectList.Add(new SelectListItem
{
Value = element.ID.ToString(),
Text = element.Name
});
}
return selectList;
}
public JsonResult ReturnJSONDataToAJax() //It will be fired from Jquery ajax call
{
CSharpCornerEntities cshparpEntity = new CSharpCornerEntities();
var jsonData = cshparpEntity.MySkills.ToList();
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
public enum MySkills
{
ASPNETMVC,
ASPNETWEPAPI,
CSHARP,
DOCUSIGN,
JQUERY
}
public struct ConvertEnum
{
public int Value { get; set; }
public String Text { get; set; }
}
}
}
Complete View (index.cshtml)
@model VariousWayBindingDropDownListInMVC5.Models.MySkills
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: "ReturnJSONDataToAJax",
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: JSON,
success: function (result) {
$(result).each(function () {
$("#FromJson").append($("<option></option>").val(this.ID).html(this.Name));
});
},
error: function (data) { }
});
});
</script>
<style>
table, th, td {
border: 1px solid black;
padding: 15px;
}
thead {
background-color: skyblue;
color: white;
}
</style>
@{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
{
Text = "ASP.NET MVC",
Value = "1"
});
listItems.Add(new SelectListItem
{
Text = "ASP.NET WEB API",
Value = "2",
Selected = true
});
listItems.Add(new SelectListItem
{
Text = "DOCUSIGN",
Value = "3"
});
listItems.Add(new SelectListItem
{
Text = "C#",
Value = "4"
});
}
<table>
<thead>
<tr>
<td>Binding Way</td>
<td>DropdownList</td>
</tr>
</thead>
<tr>
<td> Populating With Hardcoded Data</td>
<td>
@Html.DropDownList("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" },
})
</td>
</tr>
<tr>
<td>
Populating With ViewBag Data
</td>
<td>
@Html.DropDownList("MySkills", (IEnumerable<SelectListItem>)ViewBag.MySkills)
</td>
</tr>
<tr>
<td>
Populating With ViewData Data
</td>
<td>
@Html.DropDownList("MySkills", (IEnumerable<SelectListItem>)ViewData["MySkills"])
</td>
</tr>
<tr>
<td>
Populating With TempData Data
</td>
<td>
@Html.DropDownList("MySkills", (IEnumerable<SelectListItem>)TempData["MySkills"])
</td>
</tr>
<tr>
<td>
Populating With Jquery Data
</td>
<td>
@Html.DropDownList("MySkills", (IEnumerable<SelectListItem>)TempData["MySkills"])
</td>
</tr>
<tr>
<td>
Populating From Enum
</td>
<td>
@Html.DropDownList("MySkills", new SelectList(ViewBag.MySkillEnum, "Value", "Text"))
</td>
</tr>
<tr>
<td>
Populating With Database and EF
</td>
<td>
@Html.DropDownList("MySkills", (IEnumerable<SelectListItem>)ViewData["DBMySkills"])
</td>
</tr>
<tr>
<td>
Populating With Json Data
</td>
<td>
@Html.DropDownList("FromJson", new SelectList(Enumerable.Empty<SelectListItem>()))
</td>
</tr>
<tr>
<td>
Populating With Model Data
</td>
<td>
@Html.DropDownList("FromModel", Model.Skills)
</td>
</tr>
<tr>
<td>
Populating With Global static Data
</td>
<td>
@Html.DropDownList("StaticData", listItems)
</td>
</tr>
</table>
Model(MySkills.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace VariousWayBindingDropDownListInMVC5.Models
{
public class MySkills
{
public int ID { get; set; }
public string Name { get; set; }
public IEnumerable<SelectListItem> Skills { get; set; }
}
}
I hope you understood the different ways of DropDownList binding. I did attach the demo project without package for entity framework 6.0 due to file size exceeded more than 25MB.
Summary
In this Article, you learned different ways of DropDownList binding. I hope it's helpful and your valuable feedback and comments about this article are always welcome.