Нет элемента ViewData типа 'IEnumerable <SelectListItem>', который имеет ключ 'branche' - PullRequest
0 голосов
/ 07 января 2019

Я пытаюсь сделать выпадающий список, он работает, приносит данные из БД, но с ошибкой ниже, любой может помочь мне решить эту проблему, пожалуйста.

Нет элемента ViewData типа 'IEnumerable', который имеет ключ 'branche'.

.Net MVC 5

мой контроллер

    public ActionResult Create()
    {
        //sysEntities1 db = new sysEntities1();
        //ViewBag.branches = new SelectList(db.branche, "brancheID", "brancheName");



        sysEntities1 db1 = new sysEntities1();

        IEnumerable<SelectListItem> branches = db.branche.Select(
            b => new SelectListItem { Value = b.brancheName, Text = b.brancheName });
        ViewData["brancheName"] = branches;


        return View();
    }

My View

@Html.DropDownList ("branche", (IEnumerable) ViewBag.branches, "Выбрать ветвь", new {htmlAttributes = new {@class = "form-control"}})

Ответы [ 2 ]

0 голосов
/ 07 января 2019

Попробуйте проверить нижеприведенный код, который я использовал.

public IEnumerable<DropDownDTO> GetCity()
    {
        IEnumerable<DropDownDTO> objDropDown;

        using (var context = new AKJIEntities())
        {
            objDropDown = (from x in context.CityMasters
                           where x.IsDeleted == false && x.Status == true
                           orderby x.City
                           select new DropDownDTO
                           {
                               Value = x.CityID,
                               Text = x.City
                           }).AsParallel().ToList();

            return objDropDown;
        }
    }

ViewBag.CityList = new SelectList(GetCity(), "Value", "Text");

@Html.DropDownList("CityId", (SelectList)ViewBag.CityList, "Select City", new { @class = "dropdown_list" })

Надеюсь, это поможет вам.

0 голосов
/ 07 января 2019

Попробуйте использовать код, указанный ниже.

@Html.DropDownList("branche", (SelectList)ViewData["brancheName"], "Select Branch", new { htmlAttributes = new { @class = "form-control" } })

Или

@Html.DropDownList("branche", (SelectList)ViewBag.branches, "Select Branch" , new { htmlAttributes = new { @class = "form-control" } })
...