Link to home
Start Free TrialLog in
Avatar of WorknHardr
WorknHardr

asked on

SelectList using DateTimeFormatInfo CurrentInfo GetMonthName?

I trying to build a SelectList using the System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName enumeration. The code works without error, just not loading the DropDownListFor correctly. As you might expect it looks like this:

       { I = 1, M = January }
      { I = 2, M = February }
        etc...

I thought it would be nice to build it as a single-line-of-code like I did for a YearSelectList like so:

       YearsSelectList = new SelectList(Enumerable.Range(DateTime.Today.Year, 15))  

Here's my current code...
[Action]
MonthsSelectList = new SelectList(Enumerable.Range(1, 12).Select(i => new { I = i, M = System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i) }))

[View]
@Html.DropDownListFor(model => model.MonthId, new SelectList(Model.MonthsSelectList, "Value", "Text"), "-select-")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WorknHardr
WorknHardr

ASKER

Excellent! thx