在asp.net mvc中显示带有自定义超链接的日历年

Mou*_*tte 4 asp.net-mvc

我正在寻找创建一个MVC网页,以日历格式显示一年中的12个月.在这个月的每一天,我只会加粗有任何活动的日期(来自数据库的数据).带活动的日期也会链接到/ Activity/2008/12/25之类的路线

我即将尝试尝试asp.net ajax控件工具包日历控件,但想知道是否有其他人有任何建议.

Ale*_*ris 7

渲染日历并不是非常复杂.通过在System.Globalization和DateTime中使用DateTimeFormatInfo,可以检索所有必要的信息:

  • DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek
  • DateTimeFormatInfo.CurrentInfo.GetMonthName(月)
  • DateTimeFormatInfo.CurrentInfo.GetAbbreviatedDayName((星期几)dayNumber)

日历中的一个月可以在表格中呈现:

_ _ _ 1 2 3 4 
5 6 7 8 9 ...
Run Code Online (Sandbox Code Playgroud)

可以使用以下类似的方式来清除空单元的数量:

DateTime date = new DateTime(year, month, 1);
int emptyCells = ((int)date.DayOfWeek + 7 
    - (int)DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek) % 7;
Run Code Online (Sandbox Code Playgroud)

由于一个月内最多31天,开始时最多6个空单元格,因此最大Ceil(37/7)= 6行可以呈现一个月.因此,一个月内最多可​​以呈现42个单元格,其中一些将为空.

每个7(一周中的天数)单元格在表中插入一个新行.

int days = DateTime.DaysInMonth(year, month);
for (int i = 0; i != 42; i++)
{
    if (i % 7 == 0) {
        writer.WriteLine("<tr>");
        if( i > 0 ) writer.WriteLine("</tr>");
    }

    if (i < emptyCells || i >= emptyCells + days) {
        writer.WriteLine("<td class=\"cal-empty\">&nbsp;</td>");
    } else {
        writer.WriteLine("<td class=\"cal-day\"\">" + date.Day + "</td>");
        date = date.AddDays(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,当日期为活动时,只需在非空单元格中将所需的其他链接添加到所需的路径.