0

在我的 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 个代理,我遇到的问题是在它们各自的下拉框中显示与该特定代理关联的正确状态代码。

4

1 回答 1

0

我很快就发现了这个超载:

                <td>
                  @Html.DropDownListFor(model => model.Fixed[i].StateCode,
                      (SelectList)ViewBag.StateCodeList, Model.Fixed[i].StateCode)
                </td>
于 2013-04-02T16:45:06.493 回答