使用List< string>填充@ Html.DropDownList.使用MVC [英] Populate @Html.DropDownList with a List<string> using MVC

查看:74
本文介绍了使用List< string>填充@ Html.DropDownList.使用MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MVC和Razor使用列表填充@ Html.DropDownList. 波纹管是我的控制器代码.因此,我需要从ViewBag中选择列表并填写下拉列表.

I'm trying to populate a @Html.DropDownList with a List using MVC and Razor. Bellow is my code for the controller. So I need to pick the list out of my ViewBag and fill the dropdownlist.

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();
    }

感谢您的帮助.

推荐答案

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

If this is in an Editor for your model property:

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

这篇关于使用List&lt; string&gt;填充@ Html.DropDownList.使用MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆