views:

63

answers:

1

I'm trying to populate a DropDownList with values pulled from a property, and my end result right now is a list of nothing but "System.Web.Mvc.SelectListItem"s. I'm sure there's some minor step I'm omitting here, but for the life of me I can't figure out what it is.

The property GET generating the list:

public IEnumerable<SelectListItem> AllFoo {
    get {
        var foo = from g in Bar
                  orderby g.name
                  select new SelectListItem {
                     Value = g.fooid.ToString(),
                     Text = g.name
                  };

        return foo.AsEnumerable();
    }
}

The controller code:

public ActionResult Edit(string id) {
    // n/a code
    ViewData["fooList"] = new SelectList(g.AllFoo, g.fooid);

    return View(g);
}

The view code:

<%= Html.DropDownListFor(model => model.fooid, ViewData["fooList"] as SelectList) %>
A: 

EDIT: This question is very similar to one that was already asked:

http://stackoverflow.com/questions/2306527/asp-net-mvc-2-html-dropdownlistfor-confusion-with-viewmodel


Otherwise, you might find this article helpful:

http://www.nickriggs.com/posts/rendering-and-binding-drop-down-lists-using-asp-net-mvc-2-editorfor/

It uses EditorFor, but the same can be done for DisplayFor.

John Nelson
I'll take a look at those, thanks.
John Tacopina