javascript - 如何记录用户已注册到事件 - SQLite
问题描述
我正在尝试使用https://fullcalendar.io/更新日历中的事件,以显示当前用户是否已注册该事件以显示事件颜色将从绿色变为红色。
以下方法在我的服务方法中。
public List<TimeTableEvent> GetAllEvents()
{
return db.TimeTableEvents.ToList();
}
public List<EventAttendance> GetAllMembershipEvents(int membershipId)
{
var events = GetAllEvents();
var attendances = db.Attendances.Where(m => m.MembershipId == membershipId);
var membershipEvents = events.Select(e => new EventAttendance
{
Event = e,
Attendance = attendances.FirstOrDefault(a => a.TimeTableEventId == e.Id)
}).ToList();
return membershipEvents;
}
public Attendance AddMembershipToEvent(int membershipId, int eventId)
{
var attendance = new Attendance { MembershipId = membershipId, TimeTableEventId = eventId,
AttendanceStatus = AttendanceStatus.Pending };
db.Attendances.Add(attendance);
db.SaveChanges();
return attendance;
}
然后我在控制器中使用这个方法
public JsonResult GetMembershipEvents(int membershipId)
{
var events = service.GetAllMembershipEvents(membershipId);
return Json(events);
}
然后我的视图中有一个 javascript 方法。如下所示。
function FetchEventsAndRenderCalendar() {
var userId = 1;//User.Claims.FirstOrDefault(c => c.Type == "Id");
events = []; // clear existing events
$.ajax({
type: "GET",
url: "/Timetable/GetMembershipEvents/" + userId,
success: function (data) {
$.each(data, function (i, json) {
events.push(
{
id: json.event.id,
title: json.event.title,
description: json.event.description,
start: moment(json.event.start),
end: moment(json.event.end),
color: json.isRegistered?"Red":"Green",
allDay: json.event.allDay
}
);
})
我正在向一个事件添加一个用户,因此其中一个事件在符合要求时应该是红色的。但是,它仍然显示为绿色。
任何帮助将不胜感激。
解决方案
您可以尝试从此更改您的功能:
function FetchEventsAndRenderCalendar() {
var userId = 1;//User.Claims.FirstOrDefault(c => c.Type == "Id");
events = []; // clear existing events
$.ajax({
type: "GET",
url: "/Timetable/GetMembershipEvents/" + userId,
success: function (data) {
$.each(data, function (i, json) {
events.push(
{
id: json.event.id,
title: json.event.title,
description: json.event.description,
start: moment(json.event.start),
end: moment(json.event.end),
color: json.isRegistered?"Red":"Green",
allDay: json.event.allDay
}
);
})
到
// Function in which you use await should haveasync delaration
async function FetchEventsAndRenderCalendar() {
var userId = 1;//User.Claims.FirstOrDefault(c => c.Type == "Id");
events = []; // clear existing events
// await means wait for the call to finish and then proceed
const response = await fetch('http://www.example.com/Timetable/GetMembershipEvents/' + userId, {
method: 'GET',
headers: {"Content-Type": "application/json"}// Set required headers. Check Postman call you made.
});
console.log(response);
//Continue doing stuff with response
}
许多 nodejs 项目都这样做。试试这个。如果这不起作用,请尝试添加async: false
您的 ajax 调用。
推荐阅读
- reactjs - 使用 react-picky 选择菜单时出现问题
- laravel - Laravel 控制器/商店
- amazon-web-services - 如何使用 Cloudformation 将代码从 aws s3 存储桶安全地提取到 Codecommit 存储库?
- python - 为什么我的元音删除功能不起作用?(Python 2.7)
- jhipster - Jhipster 重置电子邮件
- arrays - 我们可以降低将数组中的每个用户相互比较的复杂性吗?
- python - 如何在python中有效地计算无向图中的三合会人口普查
- flutter - 在flutter中单击卡片项目时如何动态创建Listview?
- laravel - 加入 Laravel 表
- windows - Windows 自定义凭据提供程序