MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

ASP.NET MVC's HtmlHelper extension methods take out a lot of the HTML-by-hand drudgery to which MVC re-introduced us former WebForms programmers. Another thing to which MVC re-introduced us is poor documentation, after the excellent documentation for most of the rest of ASP.NET and the .NET Framework which I now realize I'd taken for granted.

I'd come to regard using HtmlHelper methods instead of writing HTML by hand as a best practice. When I upgraded a project from MVC 3 to MVC 4, several hidden fields with boolean values broke, because MVC 3 called ToString() on those values implicitly, and MVC 4 threw an exception until you called ToString() explicitly. Fields that used HtmlHelper weren't affected. I then went through dozens of views and manually replaced hidden inputs that had been coded by hand with Html.Hidden calls.

So for a dropdown list I was rendering on the initial page as empty, then populating via JavaScript after an AJAX call, I tried to use a HtmlHelper method:

@Html.DropDownList("myDropdown")

which threw an exception:

System.InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'myDropdown'.

That's funny--I made no indication I wanted to use ViewData. Why was it looking there? Just render an empty select list for me. When I populated the list with items, it worked, but I didn't want to do that:

@Html.DropDownList("myDropdown", new List<SelectListItem>() { new SelectListItem() { Text = "", Value = "" } })

I removed this dummy item in JavaScript after the AJAX call, so this worked fine, but I shouldn't have to give it a list with a dummy item when what I really want is an empty select.

A bit of research with JetBrains dotPeek (helpfully recommended by Scott Hanselman) revealed the problem. Html.DropDownList requires some sort of data to render or it throws an error. The documentation hints at this but doesn't make it very clear. Behind the scenes, it checks if you've provided the DropDownList method any data. If you haven't, it looks in ViewData. If it's not there, you get the exception above.

In my case, the helper wasn't doing much for me anyway, so I reverted to writing the HTML by hand (I ain't scared), and amended my best practice: When an HTML control has an associated HtmlHelper method and you're populating that control with data on the initial view, use the HtmlHelper method instead of writing by hand.

Published Friday, October 26, 2012 12:30 PM by pjohnson
Filed under: , ,

Comments

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

watch was a little too simple previously, however right now its basically impossible

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

watch was insanely easy before, but now its virtually impossible

Monday, March 24, 2014 8:19 AM by アリーナ フィットネス水着 レディース

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Innovative new men E-book Unveils Very Best Way To Rule The men Arena

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Listed Here Is A Solution That's In fact Assisting bag-masters To Grow

Monday, March 31, 2014 8:35 AM by スキルズ 日除け

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Cease Whining And Start your very own men Program Alternatively

Monday, March 31, 2014 10:02 PM by アビテラックス 家庭用精米機

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Halt Protesting and complaining , Start Off your own men Campaign Instead

Tuesday, April 1, 2014 9:06 AM by ベビーキャリアワンメッシュ

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Advanced watch Ebook Unveil The Best Way To Dominate The watch World

Wednesday, April 2, 2014 12:31 PM by コックスボルバ 激安

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Sick and tired of all the japan updates? We are here for you!!

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Variety of beneficial options to learn more about women before you are left behind.

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

japan will help all of us by simply including numerous distinctive features and attributes. It is a unvaluable item for all supporter of japan.

Friday, April 4, 2014 11:59 PM by iPod用モバイルオーディオケーブル

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Development-- watch May Have Major role In Virtually Any Website administration

Saturday, April 5, 2014 11:47 AM by au EXILIMケータイ

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Scene News - women Considered A Necessity At this time

Saturday, April 5, 2014 1:06 PM by レディース両手用 合皮手袋 グローブ

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Done with every japan news flashes? I'm on this site on your behalf

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Cutting edge watch Publication Unveils Very Best Way To Rule The watch Marketplace

Monday, April 7, 2014 6:03 AM by STREAM X 白ロム

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

The businesses Seemed to Laugh about japan - Today I actually laugh at them

Tuesday, April 8, 2014 12:11 AM by カトージベビーカー

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

The Only Tips On How To Grasp watch And Ways One Could Become a part of The watch Elite

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Listing of favourable things to do to learn more about women well before you're left out.

Tuesday, April 8, 2014 12:15 PM by AQUOS PHONE 携帯電話

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Pro Who Seems To Be Terrified Of men.

Wednesday, April 9, 2014 4:17 PM by アンドロイド タブレット

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Sick of every japan scoops? Our company is on this website to suit your needs

Wednesday, April 9, 2014 10:11 PM by ァイブフィンガーズ SPEED

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Exactly why no-one is debating watch and something that you ought to complete right away.

Friday, April 11, 2014 12:16 AM by ipad mini ケース

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Howcome no one is speaking about watch and because of that the things that one should do right away.

Friday, April 11, 2014 8:41 AM by お医者さんのコルセット

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Facts-- watch Can Play A Substantial role In Virtually Any Administration

Friday, April 11, 2014 10:25 AM by フォークロア調ファブリックデザイン

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Ideal technique for the men that it's possible to learn today.

Friday, April 11, 2014 11:34 PM by プロントの可愛いリボンがポイント!

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Those things Every person Need To Know In Regards To The bag Business

Saturday, April 12, 2014 9:10 AM by iphone 5c ケース

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Bring to an end Complaining , Start Off your very own men Marketing and advertising Preferably

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Info- bag Can Have An Essential role In Almost Any Website administration

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

Right Here Is A Solution That Is Even Enabling bag-pros Grow

# re: MVC's Html.DropDownList and "There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '...'

watch was extremely easy in the past, however right now its close to impossible

Sunday, April 13, 2014 5:42 PM by iPhone5 ケース カーボン ブランドケース

Leave a Comment

(required) 
(required) 
(optional)
(required)