I need a DropDownList in page so I try this way:
Action:
[HttpGet]
public ActionResult GetPoint() {
...
List<SelectListItem> zooms = new List<SelectListItem>();
for (int i = 0; i <= 21; i++) {
if (i == 9)
zooms.Add(new SelectListItem() { Selected = true, Text = i.ToString(), Value = i.ToString() });
else
zooms.Add(new SelectListItem() { Selected = false, Text = i.ToString(), Value = i.ToString() });
}
model.myselectlist = zooms;
...
return View(model);
}
And in View:
@Html.DropDownListFor(model => model.Zoom, Model.myselectlist , new { @class = "dropdown" })
So as I expected we have a DropDownList with the 9
is Selected Item.
But In the same View I need another DropDown So this is my implementation:
[HttpGet]
public ActionResult GetPoint() {
...
List<SelectListItem> places = new List<SelectListItem>();
places.Add(new SelectListItem() { Text = "NY", Value = "NY", Selected = false });
places.Add(new SelectListItem() { Text = "CA", Value = "CA", Selected = false });
places.Add(new SelectListItem() { Text = "TX", Value = "TX", Selected = false });
places.Add(new SelectListItem() { Text = "NH", Value = "NH", Selected = true });
places.Add(new SelectListItem() { Text = "NV", Value = "NV", Selected = false });
model.myselectlistII = places;
...
return View(model);
}
And in View I have:
@Html.DropDownListFor(model => model.Place, Model.myselectlistII , new { @class = "dropdown" })
So As you see I have a list that the NH
Item has selected = true
.
And I expect a dropdown list that the NH
selected but that not happend and allways the first item selected.
In view I have a weird behavior, I debug the code in the end of the Action and in the start of @Html.DropDownListFor
line in view page, every things is OK, but after this line I check again Model.myselectlistII
the selected Item has changed and all items have Selected property with false
, I don't understand what happened? where is the problem? why the first DropDownFor
did not change any thing but the second one change my list? what is your suggestion?