没有类型为«IEnumerable <selectlistitem>的ViewData元素

时间:2019-03-01 15:56:52

标签: c# asp.net-mvc

我正在尝试创建新产品,但是给我一个错误

  

下拉列表不是IEnumerable类型

我做一些测试 -更改发布到获取的方法,以查看数据是否跨URL,并且可以正常获取数据

这是控制者:

  public ActionResult Ajouter()
    {
        db = new IdentityDBEntities2();
        ViewBag.categ = new SelectList(db.Categories, "Id", "libelle");

        return View();
    }



[HttpPost]
    [ValidateAntiForgeryToken]
    [Route("Create")]

    public ActionResult Ajouter([Bind(Include = "Ida,description,image,Userid,Idc,titre")] Article article, HttpPostedFileBase postedFile)
    {
        try
        {
            if (ModelState.IsValid)
            {
                if (postedFile != null)
                {
                    db = new IdentityDBEntities2();
                    article.image = Convert.ToString(postedFile.ContentLength);
                    postedFile.InputStream.Read(System.Text.Encoding.Unicode.GetBytes(article.image), 0, postedFile.ContentLength);
                    string fileName = System.IO.Path.GetFileName(postedFile.FileName);
                    string FilePath = "~/Content/img/" + fileName;
                    postedFile.SaveAs(Server.MapPath(FilePath));
                    article.UserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
                    article.Idc = Convert.ToInt32(Request["Cab"]);
                    article.image = fileName;
                    db.Articles.Add(article);
                    ViewBag.categ = new SelectList(db.Categories, "Id", "libelle");
                    db.SaveChanges();
                    return RedirectToAction("Index");


                }
            }
            else return View(article); 


        }
        catch
        {
            return View();
        }
        return View();
    }

-这是视图中的下拉列表:

@Html.DropDownList("categ", null, "-- Select Category -- ", new { id = "subCategory" })

我已经将下拉菜单内容更改为

 @Html.DropDownList("categ",(IEnumerable<SelectListItem>)ViewBag.Cab, "-- Select Category -- ", new { id = "subCategory" })

但是不起作用。谢谢

1 个答案:

答案 0 :(得分:0)

您将类型List<Selectlistitem>分配给ViewBag.categ,则必须将Viewbag强制转换为该类型。

            //first create List<selectlistitem>
            List<SelectListItem> selectListItems = new List<SelectListItem>();
            selectListItems.Add(new SelectListItem() { Value = "", Text = "Select" });

            foreach (var item in db.Categories.ToList())
            {
                selectListItems.Add(new SelectListItem() { Value = item.Id.ToString(), Text = item.Name });
            }

            ViewBag.categ=selectListItems;

           //use it in view
           @Html.DropDownList("categ",(List<SelectListItem>)ViewBag.categ, "-- Select Category -- ", new { id = "subCategory" })

Result

//使用您的代码

 public ActionResult Ajouter()
        {
            db = new IdentityDBEntities2();
            List<SelectListItem> selectListItems = new List<SelectListItem>();

            foreach (var item in db.Categories.ToList())
            {
                selectListItems.Add(new SelectListItem() { Value = item.Id, Text = item.libelle });
            }
            ViewBag.categ = selectListItems;

            return View();
        }



        [HttpPost]
        [ValidateAntiForgeryToken]
        [Route("Create")]

        public ActionResult Ajouter([Bind(Include = "Ida,description,image,Userid,Idc,titre")] Article article, HttpPostedFileBase postedFile)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (postedFile != null)
                    {
                        db = new IdentityDBEntities2();
                        article.image = Convert.ToString(postedFile.ContentLength);
                        postedFile.InputStream.Read(System.Text.Encoding.Unicode.GetBytes(article.image), 0, postedFile.ContentLength);
                        string fileName = System.IO.Path.GetFileName(postedFile.FileName);
                        string FilePath = "~/Content/img/" + fileName;
                        postedFile.SaveAs(Server.MapPath(FilePath));
                        article.UserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
                        article.Idc = Convert.ToInt32(Request["Cab"]);
                        article.image = fileName;
                        db.Articles.Add(article);

                        List<SelectListItem> selectListItems = new List<SelectListItem>();

                        foreach (var item in db.Categories.ToList())
                        {
                            selectListItems.Add(new SelectListItem() { Value = item.Id, Text = item.libelle });
                        }
                        ViewBag.categ = selectListItems;
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                }
                else return View(article);


            }
            catch
            {
                return View();
            }
            return View();
        }

//下拉内容

@Html.DropDownList("categ",(List<SelectListItem>)ViewBag.categ, "-- Select Category -- ", new { id = "subCategory" })