1

I have a problem with HTML.DropDownList, I´m unable to set Selected item in Edit view. I found many simmilar topics, but no one helps me find a resolution.

I will get SelectList from ViewBag:

SelectList sl = new SelectList(ViewBag.EmployeesTest, "Id", "DisplayName", 9908);  

In View I will use

Html.DropDownList("employee",sl, new { @class = "form-control" })

In VS Debuger I can see list with all employees and employee with ID have property selected = true, but this employee is not set as selected in generated <select> input.

Do you have any what can be wrong?

Thanks

Alex.

Alex B.
  • 23
  • 3
  • Does by any chance your model also have a property named `EmployeesTest`? – Lennart Stoop Aug 17 '18 at 09:34
  • No, there is no property named EmployeesTest. – Alex B. Aug 17 '18 at 09:47
  • @AlexB. Can you put a breakpoint on the `.cshtml` code and check wither the given id is there or not in the list `EmployeesTest`. – vikscool Aug 17 '18 at 09:56
  • maybe this help you https://stackoverflow.com/questions/10039006/mvc-dropdownlist-selectedvalue-not-displaying-correctly?answertab=votes#tab-top – Hossein Aug 17 '18 at 10:01
  • @vikscool. Yes, this ID is there and have in SelectList object property Selected = true. – Alex B. Aug 17 '18 at 10:04
  • @AlexB. the only reason the selected value will not be displayed on the `client-side` is when the given value does not exist in the given `IEnumerable` list. Other then that i would suggest you give us a bit more detail like a bit of your controller code where the list is created and the `.cshtml` code where you have appended the `ViewBag.EmployeesTest` to the `@Html.DropDownList`. – vikscool Aug 17 '18 at 10:12

0 Answers0