ASP MVC3 - 设置多个项目的下拉列表

时间:2013-04-02 16:38:12

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 html.dropdownlistfor

在我的ASP MVC3网站的编辑页面上,有一部分代码处理附加到每个模型项的动态数量的代理。下面的代码用于创建要显示的确切数量的表行

    @for (int i = 0; i < Model.Fixed.Count; i++)
    {
        if(!String.IsNullOrWhiteSpace(Model.Fixed[i].AgentId))
        {
           <tr>
            <td>
                @Html.DropDownListFor(model => model.Fixed[i].StateCode,
                 (SelectList)ViewBag.StateCodeList)
            </td>
            <td>
                @Html.EditorFor(model => model.Fixed[i].AgentId)
                @Html.ValidationMessageFor(model => model.Fixed[i].AgentId)
            </td>
          </tr>                
        }
    }

不幸的是,我不确定如何显示与每个代理关联的属性状态代码。现在,此代码是控制器中唯一处理设置下拉列表值的区域:

        SelectList tmpList = new SelectList(new[] { "AL", "AK", "AS", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FM", "FL", "GA", "GU", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MH", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NA", "NM", "NY", "NC", "ND", "MP", "OH", "OK", "OR", "PW", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "US", "VT", "VI", "VA", "WA", "WV", "WI", "WY" });
        ViewBag.StateCodeList = tmpList;

由于可能有0到100个代理程序,我遇到的问题是在各自的下拉框中显示与该特定代理程序关联的正确状态代码。

1 个答案:

答案 0 :(得分:0)

我很快就找到了超载的重载:

                <td>
                  @Html.DropDownListFor(model => model.Fixed[i].StateCode,
                      (SelectList)ViewBag.StateCodeList, Model.Fixed[i].StateCode)
                </td>