Reputation: 467
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" })
Upvotes: 0
Views: 197
Reputation: 467
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.
Upvotes: 1
Reputation: 7482
You need to use @Html.DropDownListFor
- see this question Difference between DropDownlist or DropDownListFor Html helper
Upvotes: 1