0

This is editted !

(I tryed the provided answer as well with the ViewData approach and this hasn't solved my answer) since i can not post more than 2 links and i didn't kept all tryed suggestions this is the reason why this post does not contain all tryed methods in code.

Hello and thank you for reading this ,

I have spend over 4 hours with my teacher with this and we tryed all suggestions google gave us (over 10 different solutions) but none of them fixed it...

So what i want is to populate a dropdown with a list of objects (HotelChains). The error im getting is:

The ViewData item that has the key 'SelectedHotelChain' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.

This is my viewModel

This is my Controller

(cant post more than 2 links) My layout is like this :

Html.DropDownListFor(model => model.SelectedHotelChain, Model.HotelChains, "(boo)")

also tryed

@Html.DropDownList("HotelChains", Model.HotelChains, "(none)")

and with viewData also like this (with off course the appropiate notations in the model)

@Html.DropDownList("Options", ViewData["Options"] as SelectList, "--Select Item--")

We really tryed a lot of things also with ViewData[] and many others , this is for my dissertation (in 2 weeks) so i hope wome one can help me as i gonna have to switch to ASP if it doesn't get answered.

Kind Regards !

  • The error means only one thing as the dupe clearly explains. The value of `Model.HotelChains` is `null` (probably because you return the view in the post method and have not reassigned it). –  May 31 '16 at 13:15
  • And you 2nd and 3rd attempts will not work correctly - you cannot use the same name for the model you binding to and the `ViewData`/`ViewBag` property as explained in [this answer[(http://stackoverflow.com/questions/37161202/will-there-be-any-conflict-if-i-specify-the-viewbag-name-to-be-equal-to-the-mode/37162557#37162557) –  May 31 '16 at 13:18
  • And do not provide links to code - the code must be included in your question. –  May 31 '16 at 13:19
  • So this is hard to say , but we where trying everything in the wrong controller ... Our first solution was actually working ...Sry for the disturbance and thank you for your time ! – user5716425 May 31 '16 at 13:55

0 Answers0