kala-tamin Creating a Month Dropdown in C# ASP.NET MVC


Creating a Month Dropdown in C# ASP.NET MVC



This method seems stupid and a bit heavy; is there a more optimal way of creating the same thing (its for an MVC View Dropdown)

private List<KeyValuePair<int, string>> getMonthListDD {     get     {         var dDur = new List<KeyValuePair<int, string>>();         dDur.Add(new KeyValuePair<int, string>(1, "January"));         dDur.Add(new KeyValuePair<int, string>(2, "Febuary"));         dDur.Add(new KeyValuePair<int, string>(3, "March"));         dDur.Add(new KeyValuePair<int, string>(4, "April"));         dDur.Add(new KeyValuePair<int, string>(5, "May"));         dDur.Add(new KeyValuePair<int, string>(6, "June"));         dDur.Add(new KeyValuePair<int, string>(7, "July"));         dDur.Add(new KeyValuePair<int, string>(8, "August"));         dDur.Add(new KeyValuePair<int, string>(9, "September"));         dDur.Add(new KeyValuePair<int, string>(10, "October"));         dDur.Add(new KeyValuePair<int, string>(11, "November"));         dDur.Add(new KeyValuePair<int, string>(12, "December"));          return dDur;     } } 



ASP.Net MVC (1) - how can I pass JSON to view but not as separate ajax call

1:



How do you globalise the ASP.Net forms authentication longinUrl for multiple languages?
In your view model you could have a Months property:.
Can i put ASP.NET in Js file?
public IEnumerable<SelectListItem> Months {     get      {         return DateTimeFormatInfo                .InvariantInfo                .MonthNames                .Select((monthName, index) => new SelectListItem                {                    Value = (index + 1).ToString(),                    Text = monthName                });     } } 
which could be directly bound to a DropDownListFor:.
how can i embed a powerpoint slide into a webpage
<%= Html.DropDownListFor(x => x.SelectedMonth, Model.Months) %> 


Posting text with html tags without changing the requestValidationMode?


LINQ to SQL find average of field?

2:



Developing mvc application
Based on darins simple and elegant reply, i thought I'd share the little helper method that i refactored as a result of seeing this:.
Using OpenID (via DotNetOpenAuth) along with user roles and other Membership Provider features
public static string ComboDaysOfWeek(this HtmlHelper helper, string id, string selectedValue) {     var newitems = DateTimeFormatInfo         .InvariantInfo         .DayNames         .Select((dayName, index) => new SelectListItem         {             Value = (index + 1).ToString(),             Text = dayName,             Selected = (selectedValue == (index + 1).ToString())         });      var result = helper.DropDownList(id, newitems).ToHtmlString();     return result; } 
usage: .
<%= Html.ComboDaysOfWeek("ActivityStartDay", Model.ActivityStartDay)%> 
[edit] - this is in MVC2, therefore the .ToHtmlString() is added to the helper.DropDownList() method..


3:


You could try something like:.
Dictionary<int, string> dDur = new Dictionary<int, string>();  for (int i = 1; i <= 12; i++) {     dDur.Add(i, CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i)); }  return dDur; 
Remember and add a using directive for System.Globalization at the top of your class..


4:


Simplicity is gold, so in your View:.
<select id="month">     @for (Int16 i = 0; i < 12; i++)     {         <option value="@i">@DateTimeFormatInfo.InvariantInfo.MonthNames[i]</option>     } </select> 


5:


Why can't you use Dictionary<int, string> instead of List<KeyValuePair<int, string>>.
for(int i=1; i<=12; i++)     myDict.Add(i, CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i)); 


6:


This worked for me..
       get         {             var list = new List<SelectListItem>();             list.Add(new SelectListItem() { Text = "Select Month", Value = "0" });             list.AddRange(DateTimeFormatInfo                   .InvariantInfo                   .MonthNames                   .Where(m => !String.IsNullOrEmpty(m))                   .Select((monthName, index) => new SelectListItem                   {                       Value = (index + 1).ToString(),                       Text = monthName                   }).ToList());              return list;          } 


7:


Bind it straight to your dropdown.
for (int i = 1; i <= 12; i++) {     MonthDropdown.Items.Add(new ListItem(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(i), i.ToString())); } 



78 out of 100 based on 63 user ratings 938 reviews