Solved

SelectList using DateTimeFormatInfo CurrentInfo GetMonthName?

Posted on 2014-07-19
2
314 Views
Last Modified: 2014-07-19
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

0
Comment
Question by:WorknHardr
2 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 450 total points
ID: 40206636
2 things are going wrong:
1: You're making the "action" select list out of only one class, not specifying anything for display and value members.  So it's just inferring what it should write as text.
2: You're making another selectlist on the view, based on the first already existing (and would be valid) select list.


Specify the text and value fields in your action selectlist constructor, and then just pass that directly into the view part, instead of creating another one:


[Action]
MonthsSelectList = new SelectList(Enumerable.Range(1, 12).Select(i => new { I = i, M = System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i) }), "I", "M")

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

Open in new window

0
 

Author Closing Comment

by:WorknHardr
ID: 40206690
Excellent! thx
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question