I see a weird thing in DropDownListFor
. This is a part of My View:
1:<dt>
2: @Html.LabelFor(model => model.MainModel.DefaultAreaId)
3:</dt>
4:<dd>
5: @Html.DropDownListFor(model => model.MainModel.DefaultAreaId, Model.FKs["DefaultAreaId"].Items, new { @class = "Areadd" })
6: @Html.ValidationMessageFor(model => model.MainModel.DefaultAreaId, null, new { @class = "invalid-side-note" })
7:</dd>
I Check in Debug the Last Line of My Controller: return View(model);
And the values of Model.FKs["DefaultAreaId"].Items
is:
0: {Title="t1", value=13, Selected = false},
1: {Title="t2", value=15, Selected = false},
2: {Title="t3", value=17, Selected = false},
3: {Title="t4", value=16, Selected = true}
Also I Check it in line 2 of above view and the values is same, But Exactly after line 5 after DropDownListFor
values Change to:
0: {Title="t1", value=13, Selected = false},
1: {Title="t2", value=15, Selected = false},
2: {Title="t3", value=17, Selected = false},
3: {Title="t4", value=16, Selected = false}
I can't understand what happen with DropDownListFor
and my model values? I also use the same way in same view :
11:<dt>
12: @Html.LabelFor(model => model.MainModel.DefaultControllerId)
13:</dt>
14:<dd>
15: @Html.DropDownListFor(model => model.MainModel.DefaultControllerId, Model.FKs["DefaultControllerId"].Items, new { @class = "Controllerdd" })
16: @Html.ValidationMessageFor(model => model.MainModel.DefaultControllerId, null, new { @class = "invalid-side-note" })
17:</dd>
And everything is fine, I also change the name of DefaultAreaId
but nothing change.
So does any one have any explanation for this? what happen here?
if needed I can copy all of my View, Controller and Model.
Update
In Controller:
List<SelectListItem> Areas = new List<SelectListItem>();
Areas.Add(new SelectListItem { Text = "t1", Value = "13", Selected = false });
Areas.Add(new SelectListItem { Text = "t2", Value = "15", Selected = false });
Areas.Add(new SelectListItem { Text = "t3", Value = "17", Selected = false });
Areas.Add(new SelectListItem { Text = "t4", Value = "16", Selected = true });
Model.FKs.Add("DefaultAreaId", new ForeignKey { Title = "area", Items = Areas });
Fks
:
public Dictionary<string, ForeignKey> FKs { get; set; }
And ForeignKey
is:
public class ForeignKey {
public string Title { get; set; }
public List<SelectListItem> Items { get; set; }
}