views:

358

answers:

4

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;
    }
}
+1  A: 

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));
Veer
+1  A: 

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.

fat_tony
+6  A: 

In your view model you could have a Months property:

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:

<%= Html.DropDownListFor(x => x.SelectedMonth, Model.Months) %>
Darin Dimitrov
Wow so many ways to skin the same rabbit; But I like this one most as it 'feels' clean. I'll probably ram them all into a bulk load and see which ones fastest :op
Chris M
The only downside to this approach as written is that there's 13 month entries in MonthNames (January -> December plus a "blank" month). This might be OK for your UI to have the blank box, but if you want to have a custom selector label then you need to account for that by trimming it off.
Paul
+1  A: 

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:

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)%>

cheers

jim

[edit] - this is in MVC2, therefore the .ToHtmlString() is added to the helper.DropDownList() method.

jim
Nice job squire
Chris M
... sorted!! :) - you obvioulsy could take this approach with your month dropdown as well, rather than attaching it to your model as an alternative.
jim