0

将类添加到 html.dropdownlist 后,我​​面临以下错误。

错误: “System.Web.Mvc.HtmlHelper”不包含“DropDownList”的定义和最佳扩展方法重载“System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, string )' 有一些无效的参数

<li>
                    @Html.LabelFor(m => m.BuildType)
                    @Html.DropDownList("BuildType", new { @class = "BuildType" })
                    @Html.ValidationMessageFor(m => m.BuildType)
                </li>
                <li>@Html.LabelFor(m => m.BuildMode)
                    @Html.DropDownList("BuildMode", new { @class = "BuildMode" })
                    @Html.ValidationMessageFor(m => m.BuildMode) </li>
                <li>
4

3 回答 3

2

您的列表选项在哪里?您需要通过IEnumerable<SelectListItem>对象提供选项列表(请参阅此重载)。

所以你的模型会有这样的东西:

public IEnumerable<SelectListItem> BuildModeOptions { get; set; }

您的视图会将列表传递给助手:

@Html.DropDownList("BuildMode", Model.BuildModeOptions, new { @class = "BuildType" })

或者,由于您在For其他助手上使用类型安全版本,因此也可以DropDownListFor在此版本上使用:

@Html.DropDownListFor(m => m.BuildMode, Model.BuildModeOptions, new { @class = "BuildType" })

但请记住,Model.BuildMode 是您选择的值——Model.BuildModeOptions 用于您的下拉选项。

于 2012-11-14T15:21:35.853 回答
1

第二个参数应该是您要在下拉列表中显示的项目列表。

所以它看起来像:

    @Html.DropDownListFor("BuildType", m.yourListofItems, new { @class = "BuildType" })
于 2012-11-14T15:24:38.460 回答
1

You can use:

@Html.DropDownListFor(m => m.BuildType, (SelectList)Viewbag.YourSelectList, "Select Build type", new { @class = "BuildType" })

or

@Html.DropDownListFor(m => m.BuildType, Model.YourSelectList, "Select Build type", new { @class = "BuildType" })

When you use @Html.DropDownList, you specify a name for the dropdownlist... but you are missing the SelectList itself. I think that out of the box, the helper will try to use the name of the DropDownList (in your case "BuildType") to search in the ViewData collection for the SelectList.

When you use a @Html.DropDownListFor you don't use a name, but a lamda expression m => m.BuildType that will help you in same cases to not have harcoded names.

Your SelectList (the second parameter) can be grabbed from Viewbag or from a property in your Model.

于 2012-11-14T15:32:15.803 回答