There are a couple of posts about this on Stack Overflow but none with an answer that seem to fix the problem in my current situation.
I have a page with a table in it, each row has a number of text fields and a dropdown. All the dropdowns need to use the same SelectList data so I have set it up as follows:
Controller
ViewData["Submarkets"] = new SelectList(submarketRep.AllOrdered(), "id", "name");
View
<%= Html.DropDownList("submarket_0", (SelectList)ViewData["Submarkets"], "(none)") %>
I have used exactly this setup in many places, but for some reason in this particular view I get the error:
There is no ViewData item of type 'IEnumerable' that has the key 'submarket_0'.
I had same error, I think the problem is that the error text is confusing, because its giving a false key name.
In your case It should say "There is no ViewData item of type 'IEnumerable' that has the key "Submarkets"".
My error was a misspelling in the view code (your "Submarkets"), but the error text made me go crazy.
I post this answer because I want to say people looking for this error, like I was, that the problem is that its not finding the IENumerable, but in the var that its supposed to look for it ("Submarkets" in this case), not in the one showed in error ("submarket_0").
Accepted answer is very interesting, but as you said the convention is applied if you dont specify the 2nd parameter, in this case it was specified, but the var was not found (in your case because the viewdata had not it, in my case because I misspelled the var name)
Hope this helps!
This is OK too; For example:
==> In "NumberController" file:
==> In View file (Create.cshtml):
Now if we remove this statement:
from back of the following statement (in our controller) :
we will see this error:
There is no ViewData item of type 'IEnumerable' that has the key 'OperatorId'.
* So be sure of the existing of these statements. *
Check the Namespace.
You might assign System.Web.Webpages.Html.SelectListItem in the Controller, instead of System.Web.Mvc.SelectListItem.
Old question, but here's another explanation of the problem. You'll get this error even if you have strongly typed views and aren't using ViewData to create your dropdown list. The reason for the error can becomes clear when you look at the MVC source:
So if you have something like:
@Html.DropDownList("MyList", Model.DropDownData, "")
And
Model.DropDownData
is null, MVC looks through your ViewData for something namedMyList
and throws an error if there's no object in ViewData with that name.For me, the problem that caused this error arose when I was saving a new row to the database, but a field was null. In the database table design, that field is NOT NULL. So when I tried to save a new row with a null value for not-null field, Visual Studio threw this error. Thus, I made sure that the field was assigned a value, and the problem was fixed.
Ok, so the answer was derived from some other posts about this problem and it is:
If your
ViewData
contains aSelectList
with the same name as yourDropDownList
i.e. "submarket_0", the Html helper will automatically populate yourDropDownList
with that data if you don't specify the 2nd parameter which in this case is the source SelectList.What happened with my error was:
Because the table containing the drop down lists was in a partial view and the
ViewData
had been changed and no longer contained theSelectList
I had referenced, theHtmlHelper
(instead of throwing an error) tried to find the SelectList called "submarket_0" in the ViewData (GRRRR!!!) which it STILL couldnt find, and then threw an error on that :)Please correct me if im wrong