2

In my controller I create a list of SelectListItems and store this in the ViewData. When I read the ViewData in my View it gives me an error about incorrect types. If I manually cast the types it works but seems like this should happen automatically. Can someone explain?

Controller:

enum TitleEnum { Mr, Ms, Mrs, Dr };
var titles = new List<SelectListItem>();
foreach(var t in Enum.GetValues(typeof(TitleEnum)))
  titles.Add(new SelectListItem() 
    { Value = t.ToString(), Text = t.ToString() });

ViewData["TitleList"] = titles;

View:

// Doesn't work
Html.DropDownListFor(x => x.Title, ViewData["TitleList"])

// This Works
Html.DropDownListFor(x => x.Title, (List<SelectListItem>) ViewData["TitleList"])
dcompiled
  • 4,762
  • 6
  • 33
  • 36
  • just use: `@Html.DropDownList("TitleList")` in view, that will work without extra casting – Arvis Sep 30 '13 at 23:23

5 Answers5

3

Because ViewData is a Dictionary<string, Object>. How else could you store objects of multiple types in a keyed collection? Anything retrieved from ViewData without casting will be treated by the compiler as a base Object.

fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
1

If I remember correctly, ViewData is an array/collection of objects. This is why the extra cast is needed.

Femaref
  • 60,705
  • 7
  • 138
  • 176
0

Apparently the compiler will not perform a cast from an object of type object to another type automatically. I expect that results from ViewData are of type object at compile time. Example follows:

// This Fails
object obj = new List<SelectListItem>();
Html.DropDownListFor(x => x.Title, obj);

// This Works
var obj2 = new List<SelectListItem>();
Html.DropDownListFor(x => x.Title, obj2);
dcompiled
  • 4,762
  • 6
  • 33
  • 36
0

It's because of a feature called "static typing". Some love it, others hate it.

erikkallen
  • 33,800
  • 13
  • 85
  • 120
-1

what if you change

ViewData["TitleList"] = titles;

to

ViewData["TitleList"] = new SelectListItem() 
    { Value = t.ToString(), Text = t.ToString() };

and again try it with:

Html.DropDownListFor(x => x.Title, ViewData["TitleList"])

if this works, the I would agree with Femaref....good question though.

VoodooChild
  • 9,776
  • 8
  • 66
  • 99