Solved

SelectList using DateTimeFormatInfo CurrentInfo GetMonthName?

Posted on 2014-07-19
2
321 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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