创建月(列)周(行)日历年数据模型

时间:2014-08-15 13:44:55

标签: c# linq

我需要创建数据模型来构建以下HTML:

enter image description here

我正在考虑使用列:

Dictionary<string,int> columns = new Dictionary<string,int>();

所以我会有这样的事情:

columns.Add(0,""); // this is the day week column
columns.Add(9,"Sep");
columns.Add(10,"Oct");
columns.Add(11,"Nov");
columns.Add(12,"Dec");
columns.Add(1,"Jan");
// and so on....

然后我的行模型应该是这样的:

public class CalendarMonthDataModel
{
   public int Month {get;set;}
   public byte Index {get;set;} // this is the vertical position index to place the Day value
   public byte Day {get;set;} // this is the Day (1,2,3, etc.)
}

因此,使用该模型,如果我想填写九月数据,我会这样做:

var dataMonth = new CalendarMonthDataModel();
dataMonth.Month = 9; // september
dataMonth.Index = 6; // Day 1 starts in the position 6 (start from 0)
dataMonth.Class = "N"; // the css class (N = normal, D = disabled)
dataMonth.Day = 1; 

等等......

我的问题是:

  
      
  1. 这是生成日历数据模型的最佳方式还是另一种方式?

  2.   
  3. 如何根据我要展示的年份和月份了解行总数?

  4.   

我希望有人可以指导我解决这个难题的好方法:)。

1 个答案:

答案 0 :(得分:2)

找到解决方案:

如果有人需要做这样的事情:

var model = new List<EmployeeHolidayPeriodCalendarModel>();

            var actualDate = new DateTime(2013, 9, 1);

            // loop 12 months
            for (int index = 1; index <= 12; index++)
            {
                var date = new DateTime(actualDate.Year, actualDate.Month, 1);
                var monthInfo = new EmployeeHolidayPeriodCalendarModel();
                monthInfo.Month = (byte)date.Month;
                monthInfo.Year = date.Year;
                monthInfo.WeekDayStart = Convert.ToByte(date.DayOfWeek); // 0 = Sunday....
                monthInfo.DaysInMonth = Convert.ToByte(DateTime.DaysInMonth(date.Year, date.Month));
                monthInfo.MonthName = date.ToString("MMM");

                // increment date by adding months
                actualDate= actualDate.AddMonths(1);

                model.Add(monthInfo);
            }
            // the month where weekdaystart is first
            byte firstWeekDay = model.Min(m => m.WeekDayStart);
            byte lastWeekDay = model.Max(m => m.WeekDayStart);
            decimal maxDaysInMonth = model.Max(m => m.DaysInMonth);

            // calculate total model rows
            byte totalRows = Convert.ToByte((lastWeekDay - firstWeekDay)+maxDaysInMonth - 1);

            // fill each month data
            foreach (var month in model)
            {
                month.Items = new List<PeriodCalendarRowModel>();

                byte rowIndex = Convert.ToByte(month.WeekDayStart-firstWeekDay);
                int negRowIndex = Convert.ToInt16(rowIndex * -1);


                for (byte dayRow = 1; dayRow <= totalRows; dayRow++)
                {
                    var day = dayRow + negRowIndex;
                    if (day < 1 || day > month.DaysInMonth)
                    {
                        // add empty data
                        var newDay = new PeriodCalendarRowModel();
                        newDay.Day = 0;
                        newDay.Month = month.Month;
                        newDay.RowIndex = dayRow;

                        month.Items.Add(newDay);
                    }

                    if (day >= 1 && day <= month.DaysInMonth)
                    {
                        // add normal data
                        var newDay = new PeriodCalendarRowModel();
                        newDay.Day = (byte)day;
                        newDay.Month = month.Month;
                        newDay.RowIndex = dayRow;

                        month.Items.Add(newDay);
                    }
                }
            }