Search code examples
asp.net-mvclinqdropdownviewbag

linq with viewbag data


Controller

ViewBag.Subdivison = new SelectList(db.Retention_Model_Predictions_DS_Manual.Select(m => m.Underwriter_Name).Distinct(), "Underwriter_Name", "Underwriter_Name"); 

View

@Html.DropDownList("Underwriter_Name", null, "Please Select", htmlAttributes: new { @class = "form-control" })

Error found in Controller

"Error = Unable to evaluate the expression. Operation not supported. Unknown error: 0x80070057."

Error found in view

There is no ViewData item of type 'IEnumerable' that has the key 'Underwriter_Name'.


Solution

  • I don't think

    ViewBag.Subdivison = new SelectList(db.Retention_Model_Predictions_DS_Manual.Select(m => m.Underwriter_Name).Distinct(), "Underwriter_Name", "Underwriter_Name");
    

    is the reason you're getting the error in your controller.

    However, the error you're receiving in your view is because your ViewBag name is Subdivison but yet you're calling Underwriter_Name as the first parameter in your DropDownList method.

    Change your View code to:

    @Html.DropDownList("Subdivison", null, "Please Select", htmlAttributes: new { @class = "form-control" })
    

    You spelled subdivison wrong.. you're missing an 'i'. Subdivision.

    Hope this helps.