在完整日历中传递单个事件而不结束日期

时间:2018-04-17 11:34:49

标签: javascript c# asp.net-mvc fullcalendar

我正在尝试在日历中呈现一个事件,其开始日期和结束日期相同,即我只打算在日历的日视图中显示一行。

我尝试了两种方法;

第一种方法包括(对一个方法的ajax调用,该方法获取一对innDateTime和outDateTime,然后将这两个存储到一个列表中,该列表被发送到日历进行渲染)然而这种行为非常不整齐,因为它创建了一个不适当时段的阻止:

Index.cshtml:

//缺少条目///

                      $.ajax({
                          url: baseUrl +  'Stats/missingEntryDayJsonReturn?EnrollNumber=' + event.id + '&StartDate=' + event.start.format('YYYY-MM-DD'),
                          contenttype: 'application/json',
                          data: '',//JSON.stringify(params)
                          type: 'post'
                      }).done(function (data) {
                          console.log('bogus entry picked! ', data);

                          var calRes = $("#calendar").fullCalendar('addEventSource', data);
                      });

控制器:

 public ActionResult missingEntryDayJsonReturn(int EnrollNumber, string StartDate)
        {
            Calculations calc = new Calculations();
            List<Pair> logs = new List<Pair>();
            DateTime dt = DateTime.Parse(StartDate);
            logs = calc.getSingleDevicePairs(EnrollNumber, dt, dt, 1);
            var eventList = from e in logs
                            select new
                            {
                                id = e.id,
                                title = "Time Spent : " + (e.OutDateTime - e.InnDateTime),
                                start = ((DateTime)e.InnDateTime).ToString("s"),
                                end = ((DateTime)e.OutDateTime).ToString("s"),
                                allDay = false
                            };
            var rows = eventList.ToArray();
            return Json(rows, JsonRequestBehavior.AllowGet);
}

  public List<Pair> getSingleDevicePairs(int EnrollNumber, DateTime StartDate, DateTime EndDate, int? missingEntry)
{
if(missingEntry == 1)
{
   List<Pair> pairList = new List<Pair>();
 Pair missingPair = new Pair();
                var missingLog = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate &&
              x.Date <= EndDate && x.isIgnore != true && x.CheckType == "In").OrderByDescending(x => x.DateTime).FirstOrDefault();
                missingPair.InnDateTime = missingLog.DateTime;

missingPair.OutDateTime= missingLog.DateTime;
pairList.Add(missingPair);
                return pairList;  
}
}

输出:

enter image description here

这理解为我通过了一个列表,我现在试图只传递开始和单个条目,如:

   public ActionResult missingEntryDayJsonReturn(int EnrollNumber, string StartDate)
        {
 Calculations calc = new Calculations();
            Pair missingLog = new Pair();
            DateTime dt = DateTime.Parse(StartDate);
            missingLog = calc.getMissingEntry(EnrollNumber, dt, dt);

            var eventList = from e in missingLog
                            select new
                            {
                                id = e.id,
                                title = "Invalid Punch: " + (e.OutDateTime - e.InnDateTime),
                                start = ((DateTime)e.InnDateTime).ToString("s"),
                                end = ((DateTime)e.OutDateTime).ToString("s"),
                                allDay = false
                            };
            var rows = eventList.ToArray();
            return Json(rows, JsonRequestBehavior.AllowGet);

}
  

^^错误:无法找到查询模式的实现   来源类型&#39;对&#39; &#39;选择&#39;没找到。

 public Pair getMissingEntry(int EnrollNumber, DateTime StartDate, DateTime EndDate)
        {
                Pair missingPair = new Pair();
                var missingLog = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate &&
              x.Date <= EndDate && x.isIgnore != true && x.CheckType == "In").OrderByDescending(x => x.DateTime).FirstOrDefault();
                missingPair.InnDateTime = missingLog.DateTime;
                return missingPair;          
        }

1 个答案:

答案 0 :(得分:0)

if-clause

中您的第一种方法不正确

更改此部分:

if(missingEntry == 1)
{
   List<Pair> pairList = new List<Pair>();
 Pair missingPair = new Pair();
                var missingLog = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate &&
              x.Date <= EndDate && x.isIgnore != true && x.CheckType == "In").OrderByDescending(x => x.DateTime).FirstOrDefault();
                missingPair.InnDateTime = missingLog.DateTime;

missingPair.OutDateTime= missingLog.DateTime;
pairList.Add(missingPair);
                return pairList;  
}

到这一部分:

  if (missingEntry == 1)
            {
                Pair missingPair = new Pair();
                var missingLog = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate &&
              x.Date <= EndDate && x.isIgnore != true).OrderByDescending(x => x.DateTime).FirstOrDefault();
                if (missingLog.CheckType == "In")
                {
                    missingPair.InnDateTime = missingLog.DateTime;
                    missingPair.OutDateTime = missingLog.DateTime.AddMinutes(1);
                    pairList.Add(missingPair);
                    return pairList;
                }
                else
                {
                    return pairList;
                }
            }

为什么呢?因为这个查询:

var missingLog = db.AttendanceLogs.Where(x => x.RegisterationId == EnrollNumber && x.Date >= StartDate &&
              x.Date <= EndDate && x.isIgnore != true && x.CheckType == "In").OrderByDescending(x => x.DateTime).FirstOrDefault();
                missingPair.InnDateTime = missingLog.DateTime;

将始终使用CheckType In从数据库中选择最后一条记录,而不管最后一条记录是哪一条。加上这部分:

missingPair.OutDateTime = missingLog.DateTime.AddMinutes(1);

是为活动结束时间。

然后在你的控制器方法中:

  Calculations calc = new Calculations();
            List<Pair> logs = new List<Pair>();
            DateTime dt = DateTime.Parse(StartDate);
            logs = calc.getSingleDevicePairs(EnrollNumber, dt, dt, 1);
            if (logs.Count > 0)
            {
                var eventList = from e in logs
                                select new
                                {
                                    id = e.id,
                                    start = ((DateTime)e.InnDateTime).ToString("s"),
                                    end = ((DateTime)e.OutDateTime).ToString("s"),
                                    allDay = false,
                                    color = "#FF0000"
                                };
                var rows = eventList.ToArray();
                return Json(rows, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(0, JsonRequestBehavior.AllowGet);