7

I'm trying to understand c# ASP.NET MVC4 and keep coming across SelectList. I can't seem to find an explanation of what it is, other that this:

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

Can anyone give a simple explanation of it, and show how to use it?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
spiderplant0
  • 3,872
  • 12
  • 52
  • 91
  • http://www.aspnetmvcninja.com/views/asp-net-mvc-select-list-example – Satpal Aug 06 '13 at 13:06
  • http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc – Jason Evans Aug 06 '13 at 13:06
  • 1
    Refer to http://stackoverflow.com/questions/781987/how-can-i-get-this-asp-net-mvc-selectlist-to-work for some examples. – Phil Aug 06 '13 at 13:08
  • 2
    This is a very pertinent question for ASP.NET MVC. 'What's this thing in c#'? It's not asking to find a tool, library, or favourite off-site resource. I had the same question and found the answers here very useful! – Chalky Jun 04 '15 at 22:45

3 Answers3

7

There is a 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 view:

@Html.DropDownList("Types", ViewData["DropDownItems"] as List<SelectListItem>)
@Html.ValidationMessageFor(model => model.Types)
Mojtaba
  • 1,470
  • 4
  • 18
  • 25
  • Thanks, do you mean that dropdownItems is much the same as an object of type SelectList? Also, most of the answers & comments dont actually mention selectList class - does this mean that it is not used much and I normally you would just use a List of selectListItem? – spiderplant0 Aug 06 '13 at 13:25
  • Yes, `SelectListItem` is type of `System.Web.Mvc.SelectList`. – Mojtaba Aug 06 '13 at 13:28
3

SelectList class which contains the Key, Value pair with the Selected item to 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 dropdownlist.

Selva
  • 527
  • 4
  • 14
1

Working with drop-down lists in ASP.NET MVC has some confusing aspects, it's the reason that you find some classes that help developers to work with this prevalent object.

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

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

Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23