Solved

SelectList using DateTimeFormatInfo CurrentInfo GetMonthName?

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now