1

I researched this problem on this site and found several similar questions. I tried all of the suggestions offered but apparently, there's something about my implementation or my understanding of the explanations that causes those solutions to not work. I would appreciate some guidance on how to fix this problem.

My project consists of the following ViewModel:

 public SelectList MonthList { get; set; }


 public SelectList ReverseMonthsLists()
{
    var stringViewOfDates = GetDates().Select(_ => _.ToString("MMM yyyy")).ToList();
    var list = new List<SelectListItem>();
    list.Add(new SelectListItem { Selected = true, Text = stringViewOfDates[0], Value = stringViewOfDates[0] });

     for (int i = 1; i < stringViewOfDates.Count(); i++)
    {
        list.Add(new SelectListItem { Selected = false, Text = stringViewOfDates[i], Value = stringViewOfDates[i] });
    }

    var selectList = new SelectList(list);
    return selectList;
}


 public static IEnumerable<DateTime> GetDates()
{
    DateTime startDate = new DateTime(2017, 6, 1).Date;
    var currentDate = DateTime.Now.Date;

    int numberOfMonthsToShow = (currentDate.Year - startDate.Year) * 12 + currentDate.Month - startDate.Month;

    if (numberOfMonthsToShow == 0)
        numberOfMonthsToShow = 1;

    var dates = new List<DateTime>(numberOfMonthsToShow);
    for (int i = 0; i < numberOfMonthsToShow; i++)
    {
        dates.Add(currentDate);
        currentDate = currentDate.AddMonths(-1);
    }
    return dates;
}

My Html view contains this statement:

@Html.DropDownList("selectList", Model.ReverseMonthsLists(), "Month
Selector")

What am I doning wrong that's causing my dropdown list to fill with nothing but "System.Web.Mvc.SelectListItem"?

csharpMind
  • 89
  • 3
  • 14

2 Answers2

5

The second parameter to Html.DropDownList expects an IEnumerable<SelectListItem>, rather than a SelectList.

Try changing the method on the view model to:

    public IEnumerable<SelectListItem> ReverseMonthsLists()
    {
        var stringViewOfDates = GetDates().Select(_ => _.ToString("MMM yyyy")).ToList();
        var list = new List<SelectListItem>();
        list.Add(new SelectListItem { Selected = true, Text = stringViewOfDates[0], Value = stringViewOfDates[0] });

        for (int i = 1; i < stringViewOfDates.Count(); i++)
        {
            list.Add(new SelectListItem { Selected = false, Text = stringViewOfDates[i], Value = stringViewOfDates[i] });
        }

        return list;
    }

You could actually simplify it to:

    public IEnumerable<SelectListItem> ReverseMonthsLists()
    {
        var selectListItems = GetDates()
            .Select(_ => _.ToString("MMM yyyy"))
            .Select((dateString, index) => new SelectListItem {  Selected = index == 0, Text = dateString, Value = dateString})
            .ToList();
        return selectListItems;
    }
BenTaylor
  • 414
  • 4
  • 12
0

did you try to set data and text fields?

ASP.NET MVC Dropdown List From SelectList

var selectList = new SelectList(list, "Value" , "Text");
Roman Kiyashev
  • 150
  • 1
  • 11