首页 > 解决方案 > 如何记录用户已注册到事件 - 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
                                }
                            );
                        })

我正在向一个事件添加一个用户,因此其中一个事件在符合要求时应该是红色的。但是,它仍然显示为绿色。

任何帮助将不胜感激。

标签: javascriptc#sqlite.net-coreasp.net-ajax

解决方案


您可以尝试从此更改您的功能:

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 调用。


推荐阅读