Hello All,
In MVC (Model, View and Controllers), to create a dropdownlist control we use HTML helpers which helps us to to create various HTML and server side controls.
A dropdownlist is a collection of objects of SelectListItem class in .net which contains various list items that we can select.
Depending upon our project requirement, we may either hard code the values in code or retrieve them from a database table.
Generating dropdownliast using hard coded values:
DropDownList(string name, IEnumerable<SelectListItem> selectList, string optionLabel);
We have following code packet for this :
@{
ViewBag.Title = "Home Page";
}
<div class="jumbotron">
<h1>MVC DropDownlist Example</h1>
</div>
<div class="row">
@HtmlHelper.DropDownList("ddlDepartment", new List<selectlistitem>
{
new SelectListItem {Text = "IT", Value="1", Selected=true},
new SelectListItem {Text = "HR", Value="2"},
new SelectListItem {Text = "Management", Value="3"}
}, "--Select Department--")
</div>
0 Comment(s)