Skip to content

Instantly share code, notes, and snippets.

@kiyoaki
Last active June 29, 2017 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiyoaki/7cfd60d171d82679eaea to your computer and use it in GitHub Desktop.
Save kiyoaki/7cfd60d171d82679eaea to your computer and use it in GitHub Desktop.
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