0

I get this error

There is no ViewData item of type 'IEnumerable' that has the key 'dropdownOne'.

enter image description here

[HttpGet]
public ActionResult CreateNews()
{

    NewsDatabaseEntities db = new NewsDatabaseEntities();

    var list = db.Categories.ToList();
    ViewBag.list = (from c in list select new SelectListItem { Value = c.CategoryId.ToString(), Text = c.CategoryName.Trim() });
    return View();
}




    @Html.DropDownList("dropdownOne", (IEnumerable<SelectListItem>)ViewBag.list, new { @class = "form-control", multiple = "true" })
Lucia
  • 203
  • 5
  • 22
  • What is `dropDownOne`? – ediblecode Jul 12 '16 at 15:47
  • the DropDownList id – Lucia Jul 12 '16 at 15:48
  • @jumpingcode forget my code .... please show me how can i see solve this problem with your code – Lucia Jul 12 '16 at 15:50
  • Use a view model instead. – ediblecode Jul 12 '16 at 15:50
  • Why are you using an html helper inside of the controller? – nurdyguy Jul 12 '16 at 15:53
  • I have found that there are 2 SelectListItem classes. You may need to be clearer in both your controller and view and reference them as System.Web.MVC.SelectListItem – Steve0 Jul 12 '16 at 15:56
  • Because `ViewBag.list` is `null` (exactly the same as the dupe, but different error message because you use `DropDownList()` rather that the preferred `DropDownListFor()`) –  Jul 12 '16 at 22:24
  • @StephenMuecke Thank you so much .. You are always help me :) Can you please use my code to show me how to with DropDownListFor() – Lucia Jul 12 '16 at 23:49
  • Assuming your model has a property named `dropdownOne`, then its `@Html.DropDownListFor(m => m.dropdownOne, (IEnumerable)ViewBag.list)` however you really should be using a view model as per the dupe). But it appears you want a multiple select, so use `ListBoxFor()` (not DropDownListFor()`) and the `dropdownOne` property needs to be `IEnumerable` in order to bind the selected values correctly. –  Jul 12 '16 at 23:56
  • Thank you so much – Lucia Jul 12 '16 at 23:57

2 Answers2

0

In your action change

ViewBag.list = ... 

to

ViewBag.dropdownOne = ...

and in your View do this:

@Html.DropDownList("dropdownOne","Select a value", new { @class = "form-control", multiple = "true" })

It looks in the ViewBag for IEnumerable with key "dropdownOne".

ADyson
  • 57,178
  • 14
  • 51
  • 63
0

The most likely problem is if your dropdownlist data is null for any reason. The details are explained here but MVC will look in ViewData if the data is null and then give you the error that it can't find it in ViewData!

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'xxx'

Community
  • 1
  • 1
Lukos
  • 1,826
  • 1
  • 15
  • 29
  • @lukos..Thank you so much ... so it's better to use store the items in Session variable so i can use it when i return the view anytime instead of getting data again from database, right ? – Lucia Jul 12 '16 at 16:24
  • There are a few ways to cache the data depending on the size of it, whether the database is on the same server and how often it is accessed. If it is small data, accessed locally and/or not many times, just get it from the database and keep the session small. However, "The first rule of optimization is don't!" – Lukos Jul 12 '16 at 16:31