我正在尝试创建一个存档,括号内是每月的项目数,我想出了这样的东西:-
@{
var startMonth = DateTime.Now.Month;
var startYear = DateTime.Now.Year;
var blogList = Model.BlogViewModel.BlogArchiveList;
var monthName = DateTime.Now.ToString("MMMM");
var Counter = 0;
}
@foreach (var item in blogList)
{
if (item.BlogDate.Year == startYear)
{
if (item.BlogDate.Month == startMonth)
{
Counter = Counter + 1;
}
else
{
Counter = 1;
startMonth = item.BlogDate.Month;
}
}
else
{
Counter = 1;
startYear = item.BlogDate.Year;
startMonth = item.BlogDate.Month;
}
monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(startMonth);
@Html.ActionLink(monthName + " " + startYear + "("+ Counter +")", "Archive", new
{
year=startYear, month=startMonth}) <br/>
}
尽管这似乎可行,但如果有多个条目,我会多次写入月份。
如何避免多次写入该月,同时仍获得该月的帖子数?
感谢您的帮助和时间