将简单列表与@ html.DropDownList一起使用

时间:2019-02-14 22:45:56

标签: c# html asp.net-mvc model-view-controller

我一直在试图弄清楚如何正确使用语法,以便在MVC中加载带有简单字符串列表的下拉列表。

什么是正确的格式?

以下代码

@Html.DropDownList("VMO", List<string>(ViewBag.VMOList), new { @class = "form-control SearchCriteria", id = "VMO", name = "VMO", multiple = "multiple"})

给我错误

"Non-invocable member 'List<T> cannot be used like a method.

1 个答案:

答案 0 :(得分:1)

如果要使用Select Listlist of string来制作@Html.DropDownList(),则应如下所示:

@Html.DropDownList("VMO", new SelectlIst(ViewBag.VMOList),"Select Item", new { @class = "form-control SearchCriteria", id = "VMO", name = "VMO", multiple = "multiple"})

或者如果您想使用Select Listlist of object来制作@Html.DropDownList(),则应该如下:

@Html.DropDownList("VMO", new SelectlIst(ViewBag.VMOList,"ObjectId","ObjectName"),"Select Item", new { @class = "form-control SearchCriteria", id = "VMO", name = "VMO", multiple = "multiple"})