Solved

SelectList using DateTimeFormatInfo CurrentInfo GetMonthName?

Posted on 2014-07-19
2
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

756 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