dropdownlist,用于不选择页面加载时应选择的内容

时间:2015-04-05 05:26:26

标签: c# asp.net-mvc linq asp.net-mvc-4

我有这个助手。现在,这不是我的。这用于显示和填充下拉列表。

public static IEnumerable<SelectListItem> ToSelectList<T, TTextProperty, TValueProperty>(this IEnumerable<T> instance, Func<T, TTextProperty> text, Func<T, TValueProperty> value, Func<T, bool> selectedItem = null)
        {
            return instance.Select(t => new SelectListItem
            {
                Text = Convert.ToString(text(t)),
                Value = Convert.ToString(value(t)),
                Selected = selectedItem != null && selectedItem(t)
            });
        }

然后我有这些课程:

public class FooVm {
  public FooVm() {
    Bars = new HashSet<BarVm>();
  }

  public int FooId { get; set; }
  public IEnumerable<BarVm> Bars { get; set; }
}

public class BarVm {
  public int BarId { get; set; }
  public int BarName { get; set; }
}

然后有这个Linq查询:

var fooBar = _db.Foo
                .WithId(fooId)
                .Select(f => new FooVm {
                  FooId = f.Id,
                  Bars = f.Bars.Select(b => new BarVm {
                    BarId = b.Id,
                    BarName = b.Name
                  })
                })
                .ToList();

ViewBag.Bars = _db.Bars
                  .ToSelectList(s => s.Name, s => s.Id);

然后当我尝试渲染这些结果进行编辑/更新时。

@model ViewModels.Foo.FooVm

@Html.LabelFor(model => model.FooId)
@foreach(var item in Model.Bars) {
  @Html.DropDownListFor(model => item.BarId, ViewBag.Bars as IEnumerable<SelectListItem>, "Select bar")
}

下拉列表正常呈现,但它并未选择在页面加载时应该选择的内容。

我错过了什么?任何帮助将非常感激。感谢

1 个答案:

答案 0 :(得分:1)

您的帮助方法最多需要3个参数,但您只提供两个参数。第三个参数允许您默认设置所选项目,这将是&#39; null&#39;。例如,见下文。

ViewBag.Bars = Model.Bars.ToSelectList(s => s.BarName, s => s.BarId, s => s.BarId == 1);

修改

试试这个,但由于我不了解你的前提或你真正想要实现的目标,这只是指导你的事情。

public static MvcHtmlString DropDownListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression,
                                               IEnumerable<SelectListItem> selectList, string selectedValue, string optionLabel, object htmlAttributes)
    {
        Func<TModel, TValue> method = expression.Compile();
        string value = method(helper.ViewData.Model) as string;

        if (string.IsNullOrEmpty(value) && selectList != null)
        {
            var selectListList = selectList.ToList();
            var selectedItem = selectListList.FirstOrDefault(s => s.Selected);
            if (selectedItem == null)
            {
                var itemToSelect = selectListList.FirstOrDefault(w => w.Value == selectedValue);
                if (itemToSelect != null)
                {
                    itemToSelect.Selected = true;
                }
            }
            return helper.DropDownListFor(expression, selectListList, optionLabel, htmlAttributes);
        }

        return helper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes);
    }

像这样使用。

@Html.DropDownListFor(model => item, ViewBag.Bars as IEnumerable<SelectListItem>, item.BarId.ToString(), "Select bar", new object())

彻底测试,因为这只是一个快速原型,向您展示如何自己创建帮助。

如果您使用第一个答案,则可以覆盖所选值,因为Selected的{​​{1}}属性为SelectListItem