辅助方法DropDownListFor中的奇怪行为

时间:2012-06-09 13:51:44

标签: asp.net-mvc asp.net-mvc-3 html-helper html.dropdownlistfor

我在DropDownListFor看到一个奇怪的事情。这是我的观点的一部分:

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>

我签入调试我的控制器的最后一行:return View(model); Model.FKs["DefaultAreaId"].Items的值是:

 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}

另外,我在上面视图的第2行检查它并且值相同,但在DropDownListFor值之后的第5行之后正好改为:

 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}

我无法理解DropDownListFor和我的模型值会发生什么?我也在同一视图中使用相同的方式:

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>

一切都很好,我也改变了DefaultAreaId的名字,但没有任何改变。 那么有人对此有任何解释吗?这里发生了什么? 如果需要,我可以复制我的所有视图,控制器和模型。

更新

在控制器中:

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; }

ForeignKey是:

public class ForeignKey {
    public string Title { get; set; }
    public List<SelectListItem> Items { get; set; }
}

2 个答案:

答案 0 :(得分:1)

在将模型发送到视图之前,您应该将默认值分配给控制器中的model.MainModel.DefaultAreaId

更新:


然而,这是一个更好的方式“我假设:)” 我有一个帮助方法来在列表中设置Selected Item,如下所示:

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));
}

此处如果你想要BuildList helber,你可以随时使用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;
}
控制器中的

除了将您想要的值设置为模型外,不做任何事情:

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);
}

并在视图中像往常一样调用DropDownListFor并只调用SetSelected辅助方法:

@Html.DropDownListFor(model => model.CategoryId, ((IEnumerable<SelectListItem>)ViewData["Categories"]).SetSelected(model.CategoryId))

那就是你的例子:

@Html.DropDownListFor(model => model.MainModel.DefaultAreaId, Model.FKs["DefaultAreaId"].Items.SetSelected(model.MainModel.DefaultAreaId))

不要忘记在控制器中设置默认值。

答案 1 :(得分:0)

当您创建SelectList时,您可以在那里传递所选值或通过选择列表项目进行分配。

var selectList = new SelectList([select list items], "[Key]", "[Value]", [SelectedValue]);
var selectList = new SelectList(list, "Id", "Name", 2);

谢谢,