0

I have a drop down list that i am looking to retain the selected item after postback to the controller.

In my controller i have -

ViewBag.SectionsID = new SelectList(_sectionRepo.getSections(), "ID", "Name");

Which is used with the following dropdownlist within my razor view -

 @Html.DropDownList("SectionsID", (IEnumerable<SelectListItem>)ViewBag.SectionsID, Resources.LblPleaseSelect, htmlAttributes: new { @class = "form-control" })

My controller currently passes a viewmodel back to the view that contains a list of records that are displayed. The mentioned drop down is also in this view as a way to select various Record Sections and then sort the displayed records by section.

Currently when the view is loaded the DDL shows "please select" when a section is selected and the submit button pressed the view returns all records within that section but the ddl shows "please select" and not the selected section...

Any help would be appreciated.

  • marked as duplicate...the link you have show is not a duplicate of this question at all. Can you please elaborate?
bsod_
  • 903
  • 8
  • 27
  • Read it again carefully :) As the duplicate clearly explains - the name of the `ViewBag` property (which in your case is `SectionsID`) **CANNOT** be the same name as the property you are binding to (which in your case is also `SectionsID`) –  Apr 10 '18 at 12:58
  • OK...thanks for your input howeve that is not resolving my issue. My question is different to the duplicate you have stated. I am looking for a way to retain the selected value after a post back. Changing the viewbag property name makes no difference. – bsod_ Apr 10 '18 at 13:04
  • Of course it will, but you have not shown your model or your controller methods so I cannot guess what other mistakes you have made –  Apr 10 '18 at 13:06

0 Answers0