2
foreach (Person person in personList) {                               
  SelectListItem item = new SelectListItem();
  item.Value = person.Id;
  item.Text = person.FirstName + " " + person.LastName;                    
  items.Add(item);
}

ViewData["personSelectList"] = new SelectList(items, "Value", "Text", 4);

<%=Html.DropDownList("personId", ViewData["personSelectList"] as SelectList)%>

This code is not setting the person with Id = 4 as the selected item, but rather always picking the first item in the list as the selected item.

What step am I missing?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Adam Levitt
  • 10,316
  • 26
  • 84
  • 145
  • possible duplicate of [ASP.NET MVC Html.DropDownList SelectedValue](http://stackoverflow.com/questions/624828/asp-net-mvc-html-dropdownlist-selectedvalue) – Oskar Kjellin Aug 09 '12 at 14:23

6 Answers6

2

Have you tried setting the Selected property on the item itself? e.g.

foreach (Person person in personList)
{
    items.Add(new SelectListItem()
    {
        Value = person.Id,
        Text = person.FirstName + " " + person.LastName,
        Selected = person.Id == 4
    });
}

Update

I think you need to pass in the selected value through to the view and handle it in there instead:

ViewData["personSelectList"] = new SelectList(items, "Value", "Text");
ViewData["personId"] = 4;

View

<%= Html.DropDownList("personId", ViewData["personSelectList"] as SelectList) %> 
James
  • 80,725
  • 18
  • 167
  • 237
1
items = new List<SelectListItem>();

foreach (Person person in personList)
{
    items.Add(new SelectListItem()
    {
        Value = person.Id,
        Text = person.FirstName + " " + person.LastName,
        Selected = person.Id == 4
    });
}

ViewData["personSelectList"] = items

then view

@Html.DropDownList("holdPersonSelectList", (List<SelectListItem>)ViewData["personSelectList")

then back to controller

public ActionResult Index(string holdPersonSelectList)

edit: holdPersonSelectList in the controller will hold a string value of the Value of the selected Item. So, If it's an id field, just parse to int.

If you're looking to set a value, I would pass in a different piece of viewdata holding that number, and set it using jquery // javascript.

$('holdPersonSelectList').val('4');
samuelesque
  • 1,165
  • 13
  • 30
0

SelectListItem.Value is a string but you're passing 4 - which is an integer. Try passing "4" instead.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
0

I just wrote test using MVC3 and it works without problem:

@{
var items = new[]
                {
                    new Test {Id = 1, Name = "Jhon"}, 
                    new Test {Id = 2, Name = "Scott"}
                };

var selectList = new SelectList(items, "Id", "Name", 2);
var selectEnumerable = items.Select(x => new SelectListItem
                                             {
                                                 Selected = x.Id == 2,
                                                 Text = x.Name,
                                                 Value = x.Id.ToString()
                                             });
}

@Html.DropDownList("name", selectList)
@Html.DropDownList("name2", selectEnumerable)

On both DropDownLists selected value is Scott, so it does select second item as written in code.

Giedrius
  • 8,430
  • 6
  • 50
  • 91
0

My solution was different. I had to change SelectList to MultiSelectList

So, instead of doing the foreach, I do something like

int[] personListSelected= personList.Select(p=>(int)p.idPerson).ToArray();

IEnumerable<SelectListItem> personList = new MultiSelectList(personList, "idPerson", "namePerson",personListSelected);
Lucas
  • 599
  • 7
  • 20
0

Using LINQ you can do the following;

ViewData["personSelectList"] = new SelectList(items, "Value", "Text", items.FirstOrDefault(i => i.id == 4).id);

or better yet,

int SelectedID = 4;
ViewData["personSelectList"] = new SelectList(items, "Value", "Text", items.FirstOrDefault(i => i.id == SelectedID).id);
Josef
  • 2,869
  • 2
  • 22
  • 23
Heinrich
  • 36
  • 6