问题
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; }
}
回答1:
You should assign default value to model.MainModel.DefaultAreaId
in the controller before sending the model to the view.
Update :
However here is a better way "as i assume :)" I've a helper method to set Selected Item in the list like this:
public static IEnumerable<SelectListItem> SetSelected(this IEnumerable<SelectListItem> selectList, object selectedValue)
{
selectList = selectList ?? new List<SelectListItem>();
if (selectedValue == null)
return selectList;
var vlaue = selectedValue.ToString();
return selectList.BuildList(m => m.Text, m => m.Value, null, m => String.Equals(m.Value, vlaue, StringComparison.CurrentCultureIgnoreCase));
}
Here if you want the BuildList helber and you can use it whenever you want SelectItem list
public static IEnumerable<SelectListItem> BuildList<T>(IEnumerable<T> items, Func<T, string> text, Func<T, string> value = null, string optionalText = null, Func<T, bool> selected = null)
{
var list = items.Select(x => new SelectListItem
{
Text = text.Invoke(x),
Value = value == null
? text.Invoke(x)
: value.Invoke(x),
Selected = selected != null && selected.Invoke(x)
}).ToList();
if (optionalText != null)
list.Insert(0, new SelectListItem() {Value = "", Text = optionalText});
return list;
}
in the controller don't do anything but set the value you want to the model:
public ActionResult Edit(){
var model = new YourModel();
model.CategoryId = 4;
//.. set the dropdownlist items in the ViewData or in your model in your case
return View(model);
}
and in the view just call DropDownListFor as usual and just call for SetSelected
helper method:
@Html.DropDownListFor(model => model.CategoryId, ((IEnumerable<SelectListItem>)ViewData["Categories"]).SetSelected(model.CategoryId))
And that would be in your example :
@Html.DropDownListFor(model => model.MainModel.DefaultAreaId, Model.FKs["DefaultAreaId"].Items.SetSelected(model.MainModel.DefaultAreaId))
Just don't forget to set the default value in your Controller.
回答2:
When you are creating the SelectList are you passing in the selected value there or assigning it via the select list item.
var selectList = new SelectList([select list items], "[Key]", "[Value]", [SelectedValue]);
var selectList = new SelectList(list, "Id", "Name", 2);
Thanks,
来源:https://stackoverflow.com/questions/10961569/strange-behavior-in-helper-method-dropdownlistfor