-1

In my controller -

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

In view -

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

Ofc it's a datetime value being passed in and I was wondering how best to convert it to a ToShortDateString.

Myzifer
  • 1,116
  • 1
  • 20
  • 56

1 Answers1

2
ViewBag.DatesEnum = _db.blah.Where(w => w.Active == true)
                            .AsEnumerable()
                            .Select(i => new SelectListItem
                                        {
                                            Value = i.ID,
                                            Text = i.Date.ToShortDateString()
                                        });

Then you can display dropdown in your view.

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

It would be better if you add dropdown data to your model and skip using ViewBag.

Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156