【问题标题】:Problems with Html.DropDownList in ASP.NET RC1ASP.NET RC1 中的 Html.DropDownList 问题
【发布时间】:2009-02-27 01:36:02
【问题描述】:

由于某种原因,我的下拉列表没有保留它的选定值 - 我知道我在这里遗漏了一些简单的东西。感谢任何 cmets!

控制器

    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();
    }

并查看:

这行不通

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

这行得通

<%= Html.DropDownList("MonitoringType")%>

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    来自控制器的报价:

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

    引自页面:

    <%= Html.DropDownList("ProjectId", "-- All Projects --")%>
    

    【讨论】:

    • 感谢您的评论 - 我想我应该让它更明确一点“类别”是不同的,既是价值又是文本。真正失败的地方是 (SelectList)ViewData["MonitoringType"]
    【解决方案2】:

    看看我对类似问题的回答。使用 DropDownList(name) 以外的方法时,DropDownList 扩展方法似乎存在错误。

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

    【讨论】:

      猜你喜欢
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      相关资源
      最近更新 更多