Eric Ness
Eric Ness

Reputation: 391

Problems with Html.DropDownList in ASP.NET RC1

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")%>

Upvotes: 0

Views: 198

Answers (2)

Troy
Troy

Reputation: 1649

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

Upvotes: 0

Dmitry44
Dmitry44

Reputation: 841

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 --")%>

Upvotes: 2

Related Questions