0

I have the following code and when I update, I get the correct results out of a list of dropdowns. I.E.. if I update one of the list items to "Closed" it does exactly what it is told both in the view and the database.

But as soon as I move away from the view and then come back, I lose the "state". Is there a way I can add a "Selected" to this of do I need to re-think?

@{
    var listItems = new List<ListItem> {
    new ListItem {Text = "Open", Value = "Open"},
    new ListItem {Text = "Closed", Value = "Closed"},
    new ListItem {Text = "Standby", Value = "Standby"}};
}
@Html.DropDownList("Parks[" + (@i + 10) + "].ParkStatus", new SelectList(listItems), new { @class = "form-control" })

Edit

I think I need some kind of "Is Selected" in the following string?

@Html.DropDownList("Parks[" + (@i + 10) + "].ParkStatus", new SelectList(listItems), new { @class = "form-control" })
Bojangles
  • 467
  • 1
  • 8
  • 21

2 Answers2

1

You need to use @Html.DropDownListFor - see this question Difference between DropDownlist or DropDownListFor Html helper

Community
  • 1
  • 1
ste-fu
  • 6,879
  • 3
  • 27
  • 46
1

Worked it out and I was on the right track, it was a need for extra parameters in:

@Html.DropDownList("Parks[" + (@i + 10) + "].ParkStatus", new SelectList(listItems), new { @class = "form-control" })

It now looks like this:

@Html.DropDownList("Parks[" + (@i + 10) + "].ParkStatus", new SelectList(listItems, "Text", "Value", Model[i].ParkStatus), new { @class = "form-control" })

And is working great.

Thanks for you help ste-fu you helped point me in the right direction.

Bojangles
  • 467
  • 1
  • 8
  • 21