我正在尝试使用Visual Studio 2019在Asp.net Core Razor Pages中创建FullCalendar应用程序,但它不显示任何数据。我想单击一个日期,然后在弹出的对话框中输入一个事件。
index.cshtml:
<div style="display:none">
...
<input type="file" id="id_image">
...
</div>
<p class="btn btn-outline-success"
onclick = "document.querySelector('#id_image').click()"
onchange = "this.innerHTML = document.querySelector('#id_image')[0].files[0].name">
<i class="someicon"> Image
</p>
Index.cshtml.cs:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel='stylesheet' href='fullcalendar/fullcalendar.css' />
<script src="~/lib/jquery.min.js"></script>
<script src="~/lib/moment.js/moment.min.js"></script>
<script src='fullcalendar/fullcalendar.js'></script>
<link href='~/fullcalendar/core/main.css' rel='stylesheet' />
<link href='~/fullcalendar/daygrid/main.css' rel='stylesheet' />
<script src='~/fullcalendar/core/main.js'></script>
<script src='~/fullcalendar/daygrid/main.js'></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
initialView: 'dayGridMonth'
});
calendar.render();
});
</script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css"
rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.2/fullcalendar.min.css"
type="text/css" rel="stylesheet" />
</head>
<body>
<div id='calendar'></div>
<script>
<link href="~/lib/fullcalendar/fullcalendar.min.css" rel='stylesheet' />
</script>
<script src="~/lib/moment.js/moment.min.js"></script>
<script src="~/lib/fullcalendar/fullcalendar.min.js"></script>
<script>
$(function () {
$('#calendar').fullCalendar({});
})
</script>
<div class="container">
<div class="row">
<div class="col-xs-12">
<br />
<div id="bootstrapModalFullCalendar"></div>
</div>
</div>
</div>
<div id="myModalSave" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Save Event</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<input type="hidden" id="hdEventID" value="0" />
<div class="form-group">
<label>Title</label>
<input type="text" id="txtTitle" class="form-control" />
</div>
<div class="form-group">
<label>StartDate</label>
<div class="input-group date" id="dtp1">
<input type="text" id="txtStartDate" class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="form-group" id="divEndDate" style="display:none">
<label>EndDate</label>
<div class="input-group date" id="dtp2">
<input type="text" id="txtEndDate" class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="form-group">
<label>Description</label>
<textarea id="txtDescription" rows="3" class="form-control"></textarea>
</div>
<div class="form-group">
<label>Theme Color</label>
<select id="ddThemeColor" class="form-control">
<option value="">Default</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
<option value="black">Black</option>
<option value="green">Green</option>
</select>
</div>
<button type="button" id="btnSave" class="btn btn-success">Save</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</form>
</div>
</div>
</div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.2/fullcalendar.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.2/fullcalendar.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.2/fullcalendar.print.css"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.min.css"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<script>
$(document).ready(function() {
var events = [];
var selectedEvent = null;
FetchEventAndRenderCalendar();
function FetchEventAndRenderCalendar() {
events = [];
$.ajax({
type: "GET",
url: "/home/GetEvents",
success: function (data) {
$.each(data, function (i, v) {
events.push({
eventID: v.EventID,
title: v.Title,
description: v.Description,
startdate: moment(v.Start),
enddate: v.End != null ? moment(v.End) : null,
color: v.ThemeColor,
});
})
GenerateCalender(events);
},
error: function (error) {
alert('failed');
}
})
}
function GenerateCalendar(events) {
$('#calendar').fullCalendar('destroy');
$('#calendar').fullCalendar({
contentHeight: 400,
defaultDate: new Date(),
timeFormat: 'h(:mm)a',
header: {
left: 'prev,next today',
center: 'title',
right: 'month,basicWeek,basicDay,agenda'
},
eventLimit: true,
eventColor: '#378006',
events: events,
eventClick: function (calEvent, jsEvent, view) {
selectedEvent = calEvent;
$('#myModal #eventTitle').text(calEvent.title);
var $description = $('<div/>');
$description.append($('<p/>').html('<b>StartDate:</b>' + calEvent.startdate.format("YYYY-MM-DD HH:mm a")));
if (calEvent.end != null) {
$description.append($('<p/>').html('<b>EndDate:</b>' + calEvent.enddate.format("YYYY-MM-DD HH:mm a")));
}
$description.append($('<p/>').html('<b>Description:</b>' + calEvent.description));
$('#myModal #pDetails').empty().html($description);
$('#myModal').modal();
},
selectable: true,
select: function (start, end) {
selectedEvent = {
eventID: 0,
title: '',
description: '',
startdate: start,
enddate: end,
color: ''
};
openAddEditForm();
$('#calendar').fullCalendar('unselect');
},
editable: true,
eventDrop: function (event) {
var data = {
EventID: event.eventID,
Title: event.title,
StartDate: event.start.format('YYYY-MM-DD HH:mm A'),
EndDate: event.end != null ? event.end.format('YYYY-MM-DD HH:mm A') : null,
Description: event.description,
ThemeColor: event.color,
};
SaveEvent(data);
}
})
}
$('#btnEdit').click(function () {
//Open modal dialog for edit event
openAddEditForm();
})
$('#btnDelete').click(function () {
if (selectedEvent != null && confirm('Are you sure?')) {
$.ajax({
type: "POST",
url: '/home/DeleteEvent',
data: { 'eventID': selectedEvent.eventID },
success: function (data) {
if (data.status) {
//Refresh the calender
FetchEventAndRenderCalendar();
$('#myModal').modal('hide');
}
},
error: function () {
alert('Failed');
}
})
}
})
$('#dtp1,#dtp2').datetimepicker({
format: 'DD/MM/YYYY HH:mm A'
});
function openAddEditForm() {
if (selectedEvent != null) {
$('#hdEventID').val(selectedEvent.eventID);
$('#txtTitle').val(selectedEvent.title);
$('#txtStartDate').val(selectedEvent.start.format('YYYY-MM-DD HH:mm A'));
$('#txtEndDate').val(selectedEvent.end != null ? selectedEvent.end.format('YYYY-MM-DD HH:mm A') : '');
$('#txtDescription').val(selectedEvent.description);
$('#ddThemeColor').val(selectedEvent.color);
}
$('#myModal').modal('hide');
$('#myModalSave').modal();
}
$('#btnSave').click(function () {
//Validation/
if ($('#txtSubject').val().trim() == "") {
alert('Subject required');
return;
}
if ($('#txtStart').val().trim() == "") {
alert('Start date required');
return;
}
else {
var startDate = moment($('#txtStartDate').val(), "YYYY-MM-DD HH:mm A").toDate();
var endDate = moment($('#txtEndDate').val(), "YYYY-MM-DD HH:mm A").toDate();
if (startDate > endDate) {
alert('Invalid end date');
return;
}
}
var data = {
EventID: $('#hdEventID').val(),
Subject: $('#txtTitle').val().trim(),
Start: $('#txtStartDate').val().trim(),
Description: $('#txtDescription').val(),
ThemeColor: $('#ddThemeColor').val(),
}
SaveEvent(data);
// call function for submit data to the server
})
function SaveEvent(data) {
$.ajax({
type: "POST",
url: '/home/SaveEvent',
data: data,
success: function (data) {
if (data.status) {
//Refresh the calender
FetchEventAndRenderCalendar();
$('#myModalSave').modal('hide');
}
},
error: function () {
alert('Failed');
}
})
}
})
</script>
</body>
</html>
Models> Calendar.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using FullCalendar.Models;
namespace FullCalendar.Pages.Calendars
{
public class IndexModel : PageModel
{
private readonly DatabaseContext db;
public IndexModel(DatabaseContext _db)
{
db = _db;
}
public void OnGet()
{
}
public IActionResult OnGetFindAllEvents()
{
var events = db.MyEvents.Select(v => new
{
eventid = v.EventID,
title = v.Title,
description = v.Description,
startdate = v.StartDate.ToString("YYYY-MM-DD"),
enddate = v.EndDate.ToString("YYYY-MM-DD"),
themecolor = v.ThemeColor
}).ToList();
return new JsonResult(events);
}
}
}
为什么不显示事件数据?
答案 0 :(得分:1)
我对我的应用进行了一些更新。首先,我发现我安装了FullCalendar.io软件的两个不同版本-wwwroot目录中的fullcalendar v.5.3.0和fullcalendar v.4.3.0。因此,我删除了所有这些全日历版本,并从Full Calendar - Getting Started安装了当前版本5.3.2作为Download zip软件包。我手动安装在wwwroot / lib目录下的fullcalendar文件夹中。
我在主项目解决方案的“页面”部分(即Pages / Calendars / index.cshtml)下的“日历”文件夹中创建了一个Razor页面-在主项目区域中创建了index.cshtml,并添加了以下代码(使用脚本标签方法,如此链接中所述-Initialize with Script Tags):-
@page
@model MyApp.Pages.Calendars.IndexModel
@{
Layout = null;
}
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'/>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link href='~/lib/fullcalendar/lib/main.css' rel='stylesheet' />
<script src='~/lib/fullcalendar/lib/main.js'></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
initialVeiw: 'dayGridMonth',
headerToolbar: {
left: 'prev, next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay'
},
});
calendar.render();
});
</script>
</head>
<body>
<div id='calendar'></div>
</body>
</html>
Visual Studio中的输出解决方案如下所示:-