代码之家  ›  专栏  ›  技术社区  ›  chrisb

ASP.NET MVC-Html.DropDownList-未通过ViewData.Model设置值

  •  17
  • chrisb  · 技术社区  · 17 年前

    我们刚刚开始使用ASP.NET MVC,遇到了以下情况。这感觉很像一个bug,但如果不是,请给出解释:)

    <%=Html.DropDownList("MyList", ViewData["MyListItems"] as SelectList)%>
    <%=Html.TextBox("MyTextBox")%>
    

    //works fine
    public ActionResult MyAction(){
      ViewData["MyListItems"] = new SelectList(items, "Value", "Text"); //items is an ienumerable of {Value="XXX", Text="YYY"}
    
      ViewData["MyList"] = "XXX"; //set the selected item to be the one with value 'XXX'
      ViewData["MyTextBox"] = "ABC"; //sets textbox value to 'ABC'
    
      return View();
    }
    

    但是,当尝试通过模型加载时,文本框的值设置与预期一致,但下拉列表未获得所选项目集。

    //doesnt work
    public ActionResult MyAction(){
      ViewData["MyListItems"] = new SelectList(items, "Value", "Text"); //items is an ienumerable of {Value="XXX", Text="YYY"}
    
      var model = new {
        MyList = "XXX", //set the selected item to be the one with value 'XXX'
        MyTextBox = "ABC" //sets textbox value to 'ABC'
      }
    
      return View(model);
    }
    

    非常感谢你的建议

    7 回复  |  直到 13 年前
        1
  •  23
  •   Community Mohan Dere    9 年前

    事实上,你只需要通过 null 对于 Html.DropDownList() .

    System.Web.Mvc.Extensions.SelectExtensions 班级的 SelectInternal() 方法,它检查 无效的 如果它作为 无效的 ,它查找 SelectList 正确地

    ViewData["MyDropDown"] = new SelectList(selectListItems,
                                 "Value",
                                 "Text",
                                 selectedValue.ToString()
                             );
    

    下面是HTML视图代码。

    <%= Html.DropDownList("MyDropDown", null,
            "** Please Select **",
            new { @class = "my-select-css-class" }
        ) %>
    

    注意:我使用的是ASP.NETMVC2.0(测试版)。

    additionalViewData Html.EditorFor() method

    递上你的信 作为 anonymous 相同的属性名称 将模型的属性转换为 Html.EditorFor() 方法

    <%= Html.EditorFor(
        m => m.MyPropertyName,
        new { MyPropertyName = Model.ListItemsForMyPropertyName }
    ) %>
    

    my answer in another thread here .

        2
  •  5
  •   Todd Smith Brandon    17 年前

    经过一系列的折边和支吾,它归结为以下代码行

    if (ViewData.ModelState.TryGetValue(key, out modelState))
    

    我不确定这是错误、限制还是设计决定。但是,您可以通过以下方式进行修复:

    <%= Html.TextBox("MyTextBox", ViewData.Model.MyTextBox) %>
    
        3
  •  2
  •   IamNotaRobot    17 年前

    ViewData[“AddressTypeId”]=新的SelectList(CustomerService.AddressType_List(),“AddressTypeId”,“Name”, myItem.AddressTypeId

        4
  •  2
  •   Michael    15 年前

    我显然来晚了。但我想补充这一点,以防其他人遇到这个问题。

    在MVC2中。您可以执行以下操作来选择项目。。。

    public ActionResult Edit(int id)
            {
                Team team = _db.TeamSet.First(m => m.TeamID == id);
                var fanYear = from c in _db.FanYearSet select c;
                ViewData["FantasyYear"] = new SelectList(fanYear, "YearID", "FantasyYear", team.FanYearReference.EntityKey.EntityKeyValues[0].Value);
                var league = from c in _db.LeagueSet select c;
                ViewData["League"] = new SelectList(league, "LeagueID", "League_Name", team.LeaguesReference.EntityKey.EntityKeyValues[0].Value);
    
                return View(team);
            }
    

    第四个参数接受一个值,并将在dropdownlist中选择所需的值。在我的例子中,我有一个名为team的表,我有一个名为fanYear和league的表。

    你可以看到,我首先得到了我正在编辑的团队,然后建立了一个幻想年的下拉列表和一个联盟的下拉列表。为了确定球队的年度和联赛,我必须使用实体参考。

        5
  •  -1
  •   Junior Mayhé    16 年前

    如果ViewData无效或为空,请注意控制器的操作。

    如果你打电话 ModelState.AddModelError("MyList","Please select the profession") 您将用此警告文本替换ViewData列表内容。

    这也是为什么互联网上的人们对DropDownList上的ViewData有空问题的原因。

    底线是,注意错误模型上的ID,它们不能干扰html控件。

        6
  •  -1
  •   Aleksander Blomskøld    13 年前

    < %= Html.DropDownList("fieldAAA", 
                           new SelectList( (IEnumerable) ViewData["Users"], "Value", "Text", fieldAAA))
    
    %> 
    
        7
  •  -1
  •   akjoshi HCP    13 年前

    好的,我可能在这个线程中遗漏了一些东西,但是在C#中,以下工作:

    Html.DropDownList("ProfessionGuid", (SelectList)ViewData["Professions"])
    

    在哪里 ProfessionGuid

    Html.DropDownList("ProfessionGuid", ViewData["Professions"] AS SelectList)