0

我有一个从我的控制器中调用下拉列表返回的数据集合,如下所示:

ViewData["statuses"] = db.GetStatuses();

它的类型为“YeagerTechModel.Status[]”。

当我尝试在一个简单的下拉列表中的视图中列出该数据时,出现以下错误:

Exception Details: System.InvalidCastException: Unable to cast object of type 'YeagerTechModel.Status[]' to type 'System.Collections.Generic.List`1[YeagerTechModel.Status[]]'

我的视图设置如下:

@using (Html.BeginForm("AddSingleStatus", "Status", new AjaxOptions { HttpMethod = "POST", }))
{
    @Html.ValidationSummary(true)
    <table>
        <tr>
            <td>
                <div class="display-label">
                    @Html.LabelFor(model => Model.Description);
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => Model.Description, new SelectList((List<YeagerTechModel.Status[]>)ViewData["statuses"], "StatusID", "Description"));
                    @Html.ValidationMessageFor(model => Model.Description)
                </div>
            </td>
           </tr> 
        <tr>
            <td>
                <input type="submit" value='@Resources.Add' />
            </td>
        </tr>
    </table>
}

如何正确地将集合重新转换为简单的下拉列表?

4

1 回答 1

1

与其将其强制转换为List<YeagerTechModel.Status[]>尝试,不如将其强制转换为YeagerTechModel.Status[]. 数组实现IEnumerable,所以它应该很好地将它传递给SelectList.

于 2013-09-01T18:14:27.943 回答