Нет элемента ViewData типа 'IEnumerable ‹SelectListItem›' с ключом 'branch'

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

Нет элемента ViewData типа «IEnumerable» с ключом «branch».

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

Мой взгляд

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


person Mansour    schedule 07.01.2019    source источник


Ответы (2)


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

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

Or

@Html.DropDownList("branche", (SelectList)ViewBag.branches, "Select Branch" , new { htmlAttributes = new { @class = "form-control" } })
person Hitesh Gohel    schedule 07.01.2019
comment
Спасибо за вашу доброту, но ошибка все еще как есть. - person Mansour; 07.01.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" })

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

person Hitesh Gohel    schedule 07.01.2019