linq with viewbag data

0

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'.

asp.net-mvc
linq
dropdown
viewbag
asked on Stack Overflow Oct 5, 2017 by Hoss • edited Oct 5, 2017 by adiga

1 Answer

0

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.

answered on Stack Overflow Oct 5, 2017 by M12 Bennett

User contributions licensed under CC BY-SA 3.0