I’ve been stuck on the same DropDownList problem for days now while working on a MVC project in VS2010. The problem has become even more frustrating right now because when I copy all relevant code into a completely new, blank project, the second instance actually runs perfectly and produces exactly the result I expect…
Controller:
<HandleError()> _
Public Class HomeController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
ViewData("Message") = "Welcome to ASP.NET MVC!"
Return View()
End Function
Function About() As ActionResult
Dim configList As List(Of String) = New List(Of String)
configList.Add("10GBaseLX4")
configList.Add("10GFC")
configList.Add("10GigE")
configList.Add("100BaseFX")
configList.Add("Test")
ViewData("cprotocols") = New SelectList(configList)
Return View()
End Function
End Class
View:
<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
About Us
</asp:Content>
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>About</h2>
<p>
<% Using Html.BeginForm()%>
<%= Html.DropDownList("cprotocols") %>
<% End Using %>
</p>
</asp:Content>
The page doesn’t do anything right now other than simply display a dropdownlist with the options shown hard-coded in the controller, but I’m not even going to bother with writing logic when my current site refuses to even show the list to the user.
The error message is There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'cprotocols'. but once again, there are no runtime errors when i copy this bit of code over to a blanked solution.
Any ideas how to solve this or even any clues on what is causing such an inconsistency? I don’t want to copy over all the other files from my current project into the new one just to have the problem manifest itself again somewhere down the line (which is bound to happen when the code is only half broken…)
Turns out it was a problem with the module; the code itself is fine. I had to delete Temporary ASP.NET files, clean up my GAC, as well as nuke all current assembly references in my project, clean solution, re-add required references, and finally clean and rebuild. I’m still not clear on why exactly VS2010 was referring to assemblies from an old build (simply cleaning and rebuilding is not enough). I also don’t know at what point the environment stops “refreshing” and decides to simply refer to obsolete builds. I’ll try to do a bit more research on this point and then add on to this post.