C# 附加信息:没有类型为';IEnumerable<;选择列表项>;Asp.NETMVC

C# 附加信息:没有类型为';IEnumerable<;选择列表项>;Asp.NETMVC,c#,asp.net-mvc,syntax-error,C#,Asp.net Mvc,Syntax Error,我遇到了一个问题, 请帮我做这个 错误:其他信息:没有具有键的“IEnumerable”类型的ViewData项 MyAction代码 最后是好的一面 ViewBag属性称为countrydrop,您试图将ViewBag.country强制转换为不存在的IEnumerable 您可以使用: @Html.DropDownList("country",(IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country") C

我遇到了一个问题, 请帮我做这个
错误:其他信息:没有具有键的“IEnumerable”类型的ViewData项

MyAction代码

最后是好的一面


ViewBag属性称为
countrydrop
,您试图将
ViewBag.country
强制转换为不存在的
IEnumerable

您可以使用:

@Html.DropDownList("country",(IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")
CSHTML文件:

    public ActionResult DropDownListTest()
    {
        var myList = new List<SelectListItem>();

        myList.Add(new SelectListItem { Text = "Darren one", Value = "1" });
        myList.Add(new SelectListItem { Text = "Darren two", Value = "2" });
        myList.Add(new SelectListItem { Text = "Darren three", Value = "3" });

        ViewBag.DarrenList = myList;

        return View();
    }
@Html.DropDownList("DarrenId",(IEnumerable<SelectListItem>)ViewBag.DarrenList)
@Html.DropDownList(“DarrenId”,(IEnumerable)ViewBag.DarrenList)

ViewBag属性被称为
countrydrop
,您试图将
ViewBag.country
强制转换为不存在的
IEnumerable

您可以使用:

@Html.DropDownList("country",(IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")
CSHTML文件:

    public ActionResult DropDownListTest()
    {
        var myList = new List<SelectListItem>();

        myList.Add(new SelectListItem { Text = "Darren one", Value = "1" });
        myList.Add(new SelectListItem { Text = "Darren two", Value = "2" });
        myList.Add(new SelectListItem { Text = "Darren three", Value = "3" });

        ViewBag.DarrenList = myList;

        return View();
    }
@Html.DropDownList("DarrenId",(IEnumerable<SelectListItem>)ViewBag.DarrenList)
@Html.DropDownList(“DarrenId”,(IEnumerable)ViewBag.DarrenList)

没有类型为“IEnumerable”的ViewData项具有key@GündoğduYakıcı-请查看我的dotnetfiddle和我从网站上获取的其他代码database@GündoğduYakıcı-没关系,您可以在文本中使用您的数据库值和值属性,如我的代码所示。@GündoğduYakıcı,这个答案是正确的,如果由于ViewBag属性为
null
,而仍然出现错误-可能是因为您提交了表单,然后返回了视图,但没有重新填充
ViewBag
属性,但您的代码毫无意义-根据您显示的视图,它应该是
@Html.DropDownListFor(m=m.carid,Model.DropDownItems)
并设置
下拉项
属性。没有类型为“IEnumerable”的ViewData项具有key@GündoğduYakıcı-请查看我的dotnetfiddle和我从网站上获取的其他代码database@GündoğduYakıcı-这无关紧要,您可以在文本和值属性中使用数据库值,如我的代码所示@GündoğduYakıcı,这个答案是正确的,如果您仍然得到错误是因为ViewBag属性为
null
——可能是因为您提交表单然后返回视图,但是没有重新填充
ViewBag
属性,但是您的代码没有意义——根据您显示的视图,应该是
@Html.DropDownListFor(m=m.carid,Model.DropDownItems)
并设置
DropDownItems
属性。
@Html.DropDownList("country",(IEnumerable<SelectListItem>)ViewBag.countrydrop,"Select country")
    public ActionResult DropDownListTest()
    {
        var myList = new List<SelectListItem>();

        myList.Add(new SelectListItem { Text = "Darren one", Value = "1" });
        myList.Add(new SelectListItem { Text = "Darren two", Value = "2" });
        myList.Add(new SelectListItem { Text = "Darren three", Value = "3" });

        ViewBag.DarrenList = myList;

        return View();
    }
@Html.DropDownList("DarrenId",(IEnumerable<SelectListItem>)ViewBag.DarrenList)