0

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.

Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
PackersFan
  • 43
  • 6
  • The error means that the value of `list` is `null` - you did not populate it (and if this occurs only when you submit the form and return the view, then its because you did not assign it in the POST method) –  Mar 15 '17 at 06:42
  • And as a side note, using `new SelectList()` to create an identical `IEnumerable` from the existing one is pointless extra overhead - just use `ViewBag.dataSourcesDropDownList = dataSourceDDL;` and in the view `@Html.DropDownList("dataSourceDDL", (IEnumerable)ViewBag.dataSourcesDropDownList)` –  Mar 15 '17 at 06:44
  • @StephenMuecke The list is (for now) independent of the form. It should have values as soon as the page loads. If I move the exact same dropdownlist code for the view to the Index.cshtml view, and the exact same controller code to populate it to the HomeController.cs controller, the dropdown will load just fine on that page. It's when I try to move it to other pages that it no longer works. – PackersFan Mar 15 '17 at 06:53
  • It will have (assuming your using `@Html.Action("getDataSourcesForDDL", yourControllerName)` in the view). But all this is awful practice - using a partial just for that, not using the strongly typed `DropDownListFor()` method, not having any validation etc. Your view model in the view should contain a property to bind to and an `IEnumerable` property for the options. And also suggest you read [this question/answer}(http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) to understand the correct approach. –  Mar 15 '17 at 06:57
  • And all you need to generate the `SelectList` is one line of code - `ViewBag.dataSourcesDropDownList = new SelectList(reportContext.getDataSources());` –  Mar 15 '17 at 06:59
  • I agree with @StephenMuecke `DropDownListFor()` would be much more elegant. – NotTelling Mar 15 '17 at 07:09

0 Answers0