【问题标题】:ViewBag select lists datetime to dateViewBag 选择列表日期时间到日期
【发布时间】:2013-01-23 14:14:15
【问题描述】:

在我的控制器中 -

ViewBag.DatesEnum = new SelectList(_db.blah.Where(w => w.Active == true).AsEnumerable(), "ID", "Date");

在视图中-

@Html.DropDownListFor(m => m.blahDate, (SelectList)ViewBag.DatesEnum)

Ofc 这是一个传入的日期时间值,我想知道如何最好地将其转换为 ToShortDateString。

【问题讨论】:

    标签: asp.net-mvc-4 selectlist viewbag


    【解决方案1】:
    ViewBag.DatesEnum = _db.blah.Where(w => w.Active == true)
                                .AsEnumerable()
                                .Select(i => new SelectListItem
                                            {
                                                Value = i.ID,
                                                Text = i.Date.ToShortDateString()
                                            });
    

    然后您可以在视图中显示下拉菜单。

    @Html.DropDownListFor(m => m.blahDate, 
                               (IEnumerable<SelectListItem>)ViewBag.DatesEnum)
    

    如果您将下拉数据添加到模型中并使用 ViewBag 跳过会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 2012-09-11
      • 2013-10-02
      • 1970-01-01
      • 2020-04-23
      相关资源
      最近更新 更多