Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Linq;
using System.Web.Mvc;
namespace SampleApp.Helpers
{
public static class HtmlHelperExtensions
{
public static SelectListItem[] Months(this HtmlHelper htmlHelper, DateTime from, DateTime to, DateTime selected)
{
return Enumerable.Range(0, (to.Year - from.Year) * 12 + (to.Month - from.Month + 1))
.Select(m => new DateTime(from.Year, from.Month, 1).AddMonths(m))
.OrderByDescending(x => x)
.Select(x =>
{
var listItem = new SelectListItem
{
Text = x.ToString("yyyy年MM月"),
Value = x.ToString("yyyy-MM-dd")
};
if (x == selected)
{
listItem.Selected = true;
}
return listItem;
}).ToArray();
}
public static SelectListItem[] Ints(this HtmlHelper htmlHelper, int from, int to, int selected)
{
if (from >= to)
{
return new SelectListItem[0];
}
return Enumerable.Range(from, to - from)
.Select(x =>
{
var listItem = new SelectListItem
{
Text = x.ToString(),
Value = x.ToString()
};
if (x == selected)
{
listItem.Selected = true;
}
return listItem;
}).ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment