asp.net-mvc - 使用 MVC 使用 List<string> 填充 @Html.DropDownList

标签 asp.net-mvc asp.net-mvc-3 drop-down-menu html-select

我正在尝试使用 MVC 和 Razor 使用列表填充 @Html.DropDownList。
波纹管是我的 Controller 代码。所以我需要从我的 ViewBag 中选择列表并填写下拉列表。

public ActionResult Register()
    {
        sparklingEntities context = new sparklingEntities();
        var query = (from discs in context.Disciplines
                     select discs).ToList();
        List<string> listOfDiscs = new List<string>();
        foreach (var item in query)
        {
            listOfDiscs.Add(item.Discipline);
        }
        ViewBag.ListOfDisciplines = listOfDiscs;
        return View();
    }

谢谢您的帮助。

最佳答案

如果这是在您的模型属性的编辑器中:

@Html.DropDownList("", new SelectList(ViewBag.ListOfDisciplines, Model))

关于asp.net-mvc - 使用 MVC 使用 List<string> 填充 @Html.DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6333270/

相关文章:

c# - 如何使用 Advanced Rest Client 发出 POST 请求

c# - 限制 Linq 列表中返回的结果数

CSS 多重伪选择器和波浪号,如何

c# - 网站发布过程中未在项目中找到数据库

.NET 生成服务引用时加载错误的程序集

javascript - 如何使用 asp.net MVC 在挖空组件中隐藏基于角色的部分

asp.net-mvc-3 - 限制对 ASP.NET MVC 3 中的 Controller 方法的访问

asp.net-mvc-3 - EF 和存储库模式 - 最终在一个 Controller 中出现多个 DbContext - 有任何问题(性能、数据完整性)吗?

php - 使用 MySQL 表填充的 PHP 下拉列表将选择插入到另一个表中

javascript - 将元素列为下拉菜单 : fails on Safari