What is the SelectList class in C #?

I am trying to understand C # ASP.NET MVC4 and continue to encounter SelectList . It seems that I can not find an explanation of what it is, another:

http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist%28v=vs.108%29.aspx

Can someone give a simple explanation for this and show how to use it?

+7
c # asp.net-mvc
source share
3 answers

There is simple code that I used for dropdownlist in asp.net mvc:

In Controller :

  List<SelectListItem> dropdownItems = new List<SelectListItem>(); dropdownItems.AddRange(new[]{ new SelectListItem() { Text = "Option One", Value = "1" }, new SelectListItem() { Text = "Option Two", Value = "2" }, new SelectListItem() { Text = "Option Three", Value = "3" }}); ViewData.Add("DropDownItems", dropdownItems); 

And, in cshtml :

 @Html.DropDownList("Types", ViewData["DropDownItems"] as List<SelectListItem>) @Html.ValidationMessageFor(model => model.Types) 
+7
source share

A SelectList class that holds a Key, Value pair with the selected item in True.

For example,

 listItems.Add(new SelectListItem { Text = xElement.Element("text").Value, Value = xElement.Element("value").Value }); var selected = listItems.Where(x => x.Value == "Test1").First(); selected.Selected = true; 

This sample, which helps to get the selected value in the drop-down list.

+3
source share

Working with drop-down lists in ASP.NET MVC has some confusing aspects; this is the reason you will find some classes that help developers work with this common object.

There is a great blog post which I think clearly describes SelectList.

http://odetocode.com/Blogs/scott/archive/2010/01/18/drop-down-lists-and-asp-net-mvc.aspx

+1
source share

All Articles