I am trying to create an Archive, with the number of items per month inside parenthesis, and I have come up with something like this:-
@{
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/>
}
Although this seems to work, I am getting the month written multiple times if there is more than one entry.
How can I avoid writing the month multiple times, while still getting the count of posts for that month?
Thanks for your help and time