不包含 'DropDownList' 的定义

does not contain a definition for 'DropDownList'

更新:

因为我的 DropDownTypes 是一个集合,所以我尝试传递这样的东西:它有效,但我遇到的唯一问题是它选择默认值,即 0 的索引和我的item.Type是选择的值

 @Html.DropDownListFor(m => item.Type, new SelectList(item.DropDownTypes.Select(x => x.Text)))

我在这里做错了什么,我正在尝试加载下拉列表

'System.Web.Mvc.HtmlHelper>' does not contain a definition for 'DropDownList' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable, object)' has some invalid arguments

@model IEnumerable<web.Models.CollectionViewModel>
@foreach (var item in Model )
{
  <tr>
    <td>
         @Html.DropDownList(item.DropDownTypes, (IEnumerable<SelectListItem>)ViewBag.DropDownLoadRecoveryType, new { @class = "form-control" })
    </td>
  </tr>
}

您使用的 DropDownList 助手的第一个参数必须是一个字符串,因此请确保 item.DropDownTypes 实际上是一个字符串。如果不是,您可以考虑对其调用 .ToString()

您应该考虑将下拉列表的选定值添加到模型的另一个变量中。这样您就可以在下拉列表中使用它,因为您目前正在传递一个无效的列表:

@model IEnumerable<web.Models.CollectionViewModel>
@foreach (var item in Model )
{
  <tr>
    <td>
         @Html.DropDownList(item.SelectedDropDownType, (IEnumerable<SelectListItem>)ViewBag.DropDownLoadRecoveryType, new { @class = "form-control" })
    </td>
  </tr>
}

item.SelectedDropDownType 可能是

public string SelectedDropDownType { get; set;}

这就是我能够达成解决方案的方式,以防其他人需要知道。

@Html.DropDownListFor(model => item.Type.Value, new SelectList(ViewBag.DropDownLoadRecoveryType,  "Value", "Text", item.Type))