Code to render a dropdown list in Asp.Net MVC

Goud.Kv
Posted by Goud.Kv under ASP.NET MVC category on | Points: 40 | Views : 2900
Below code snippet renders the dropdown list in the view of your application.

Type below code in your Razor view,

@Html.DropDownList("Categories", new List<SelectListItem>
{
new SelectListItem {Text = "Item 1", Value = "1", Selected = true},
new SelectListItem {Text = "Item 2", Value = "2"},
new SelectListItem {Text = "Item 3", Value = "3"},
new SelectListItem {Text = "Item 4", Value = "4"},
new SelectListItem {Text = "Item 5", Value = "5"},
new SelectListItem {Text = "Item 6", Value = "6"}
}, "Select Category")


The above code generates a dropdown list with items 1 to 6., Selected 'true' property shows the specific item as default after the page load.

Comments or Responses

Login to post response