Problems with presenting MVC2 ViewData

I am trying to pass a list of several items to a view via ViewData to create a drop down list. This should not be too complicated, but I'm new to MVC, so I probably don't see anything obvious.

The controller assigns a ViewData list:

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

and the view tries to display it in the drop-down list:

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

However, when I run it, I get this error: There is no ViewData element of type "IEnumerable" that has a location key.

Any ideas why this is not working? Also, shouldn't he look for the key "ImageLocations", not the location?

+3
source share
2 answers

If you use:

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

and

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

.

( i) ViewData (ImageLocatons = > ImageLocations). , , DropDownList, null. MVC .

+7

, ViewData reset?

, .
ViewData [ "ImageLocations" ].

, , .

0

Source: https://habr.com/ru/post/1726897/


All Articles