使用Twitter Bootstrap样式和C#中的项填充@ html.DropDownListFor()

Gma*_*n16 4 c# html.dropdownlistfor asp.net-mvc-4 twitter-bootstrap visual-studio-2012

我正在尝试填充下拉列表中的项目.我在visual studio 2012上使用MVC4和twitter bootstrap样式.

我对控制器的代码是:

String[] genders =  { "Male","Female","Other"};
ViewBag.genders = new SelectList(genders);
Run Code Online (Sandbox Code Playgroud)

在我看来是:

@Html.DropDownListFor("genders", "Select a gender", new { @class = "form-control" })
Run Code Online (Sandbox Code Playgroud)

但是,我收到一个错误:

'无法推断方法'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression>,System.Collections.Generic.IEnumerable,object)'的类型参数从用法.尝试明确指定类型参数.

我尝试过其他各种形式的陈述.但基本上我想保持控制器不变并编辑视图中的语句.但我愿意接受任何人们愿意提出的建议,以帮助我解决这个问题.

提前致谢

ced*_*lof 15

试试这个:

@Html.DropDownListFor(m=>m.SelectedGender, (SelectList)ViewBag.genders, "Select gender", new { @class = "form-control" })
Run Code Online (Sandbox Code Playgroud)

m=>m.SelectedGender,将值绑定到视图模型中的某些内容(或者如果没有模型,则应使用@Html.DropDownList提供字符串).