0

For some reason my drop down list is not retaining it's selected value - I know I am missing something simple here. Thanks for any comments!

Controllers

    public ActionResult Test()
    {
        ViewData["MonitoringType"] = new SelectList(myModel.GetMonitoringType(), "Category", "Category");
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Test(FormCollection formValues)
    {
        ViewData["MonitoringType"] = new SelectList(myModel.GetMonitoringType(), "Category", "Category", formValues["MonitoringType"]);
        return View();
    }

And View:

This doesn't work

<%= Html.DropDownList("MonitoringType", (SelectList)ViewData["MonitoringType"],new {style = "width: 300px;"})%>

This works

<%= Html.DropDownList("MonitoringType")%>
Eric Ness
  • 391
  • 1
  • 4

2 Answers2

2

quote from controller:

    var projects = from project in DB.Projects
                   orderby project.Name
                   select new { project.Id, project.FullName };
    ViewData["ProjectId"] = new SelectList(projects, "Id", "FullName", selectedProjectId);

quote from page:

<%= Html.DropDownList("ProjectId", "-- All Projects --")%>
Dmitry44
  • 831
  • 1
  • 7
  • 9
  • Thanks for the comment - I guess I should have made it a bit more explicit "Category" is distinct and is both the value and the text. And really where it is failing is the (SelectList)ViewData["MonitoringType"] – Eric Ness Feb 25 '09 at 19:19
0

Take a look at my answer to a similar question. It appears there is a bug in the DropDownList extension method when using methods other than DropDownList(name).

Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item

Community
  • 1
  • 1
Troy
  • 1,640
  • 10
  • 16