Model-View-Controller – MVC2 ViewData issue

I am trying to pass some list of items to the view via ViewData to create a drop-down list. This shouldn’t be too difficult, but I’m new to MVC, so I might be missing some obvious Stuff.

The controller assigns the list to ViewData:

ViewData["ImageLocatons"] = new SelectList(gvr.ImageLocations);< /pre> 

And the view tries to present it in the drop-down list:

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

But when I run it, I get this error:
No ViewData item of type "IEnumerable" has a "location" key.

Any Thoughts why this doesn't work? Also, shouldn't it look for the key "ImageLocations" instead of locations?

If you use:

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

and

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

Your life will be much easier.

When setting the ViewData in the example, also check for spelling errors (missing i) (ImageLocatons => ImageLocations). This will cause the pass The second parameter given to DropDownList is null. This will cause the MVC engine to search for Location.

I am trying to pass some list of items to the view via ViewData to create a drop-down list. It shouldn’t be too difficult, but I’m new to MVC, so I might be missing something obvious.

The controller assigns the list to ViewData:

< /p>

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

And the view tries to present it in the drop-down list:

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

But when I run it, I get this error:
No The ViewData item of type "IEnumerable" has a "location" key.

Any ideas why this doesn't work? Also, shouldn't it look for the key "ImageLocations" instead of locations?

If you use:

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

and

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

Your life will It’s a lot easier.

When setting the ViewData in the example, also check for spelling errors (missing i) (ImageLocatons => ImageLocations). This will cause the second parameter passed to the DropDownList to be null. This will Cause the MVC engine to search for Location.

Leave a Comment

Your email address will not be published.