So, the MVC project I'm working on I'm quickly learning that the top level design is what holds me back.. i.e., the communication from my view to controller(s).
I'm trying to display a drop down menu to the user when the page loads that will hold titles of stored procedures stored in a database. However, when the page loads, I get the following error message:
An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code
Additional information: There is no ViewData item of type 'IEnumerable' that has the key 'dataSourceDDL'.
The code I currently have in my view is as follows:
@{
SelectList list = ViewBag.dataSourcesDropDownList;
}
@Html.DropDownList("dataSourceDDL", list)
My controller has the following code:
public ActionResult getDataSourcesForDDL()
{
ReportingContext reportContext = new ReportingContext();
List<String> dataSources = reportContext.getDataSources();
List<SelectListItem> dataSourceDDL = new List<SelectListItem>();
foreach (String dataSource in dataSources)
{
dataSourceDDL.Add(new SelectListItem() { Text = dataSource, Value = dataSource });
}
ViewBag.dsDDL = dataSourceDDL;
ViewBag.dataSourcesDropDownList = new SelectList(dataSourceDDL, "Value", "Text");
return PartialView("selectDataSource");
}
The odd thing about the error I'm encountering is that if I copy and paste this exact code onto my Index.cshtml page and into the HomeController, the drop down list will generate just fine. So I have a gut feeling that it's an issue of the addReport.cshtml view not contacting the addReport.cs controller for the initial load to get these values, especially since like I said I have seen this drop down load.
I have additional code inside both my controller (methods to handle a user inserting a new report) and my view (just an html form that allows the user to submit a new report) that I don't think would be the root of the issue, but maybe it is.