Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC2 ViewData Problems

I'm trying to pass a list of a few items to a view via the ViewData to create a drop down list. This shouldn't be too difficult, but I'm new to MVC, so I'm probably missing something obvious.

The controller assigns the list to the ViewData:

ViewData["ImageLocatons"] = new SelectList(gvr.ImageLocations);

and the view tries to render it into a drop down list:

<%= Html.DropDownList("Location", ViewData["ImageLocations"] as SelectList) %>

However, when I run it, I get this error: There is no ViewData item of type 'IEnumerable' that has the key 'Location'.

Any ideas why this isn't working? Also, shouldn't it be looking for the key "ImageLocations" rather than location?

like image 933
Joe Avatar asked Dec 07 '25 10:12

Joe


1 Answers

If you use:

ViewData["Location"] = new SelectList(gvr.ImageLocations); 

and

<%= Html.DropDownList("Location") %> 

Your life will be a lot easier.

Also check out the typo (missing i) when setting the ViewData in your example (ImageLocatons => ImageLocations). This causes the second parameter you pass to DropDownList to be null. This will cause the MVC engine to search for Location.

like image 54
GvS Avatar answered Dec 10 '25 02:12

GvS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!