Пример #1
0
        public ActionResult Add(EventsFormViewModel viewModel)
        {
            // Date format must be valid
            if (viewModel.CorrectDateFormat() == false)
            {
                ModelState.AddModelError("Date", "The date format is invalid.");

                // Events list is not null
                viewModel.EventTypes = _context.EventTypes.ToList();

                return(View("EventForm", viewModel));
            }

            // Time format must be valid
            if (viewModel.CorrectTimeFormat() == false)
            {
                ModelState.AddModelError("Time", "The time format is invalid.");

                // Events list is not null
                viewModel.EventTypes = _context.EventTypes.ToList();

                return(View("EventForm", viewModel));
            }

            // Ensure form validation
            if (!ModelState.IsValid || viewModel.DateTimeAvailable() == false)
            {
                ModelState.AddModelError("Date", "The date/time is not available.");
                ModelState.AddModelError("Time", "The date/time is not available.");

                // Events list is not null
                viewModel.EventTypes = _context.EventTypes.ToList();

                return(View("EventForm", viewModel));
            }

            // Creat an Event object from the information from the form for the database
            var e = new Event
            {
                EventOwnerId  = User.Identity.GetUserId(),
                DateTime      = viewModel.GetDateTime(),
                EventTypeId   = viewModel.EventType,
                EventLocation = viewModel.EventLocation,
                EventName     = viewModel.EventName,
                Description   = viewModel.Description
            };

            // Add to database
            _context.Events.Add(e);
            _context.SaveChanges();

            if (viewModel.Attending)
            {
                return(RedirectToAction("Attend", "Attendance", new { eventId = e.Id }));
            }

            return(RedirectToAction("MyEvents", "Events"));
        }
Пример #2
0
        // GET: Events
        public ActionResult Create()
        {
            var viewModel = new EventsFormViewModel
            {
                Types = _context.Types.ToList()
            };

            return(View(viewModel));
        }
Пример #3
0
        public ActionResult Add()
        {
            var viewModel = new EventsFormViewModel
            {
                Heading    = "Create an Event",
                EventTypes = _context.EventTypes.ToList()
            };

            return(View("EventForm", viewModel));
        }
Пример #4
0
        public ActionResult Edit(EventsFormViewModel viewModel)
        {
            // Date format must be valid
            if (viewModel.CorrectDateFormat() == false)
            {
                ModelState.AddModelError("Date", "The date format is invalid.");

                // Events list is not null
                viewModel.EventTypes = _context.EventTypes.ToList();

                return(View("EventForm", viewModel));
            }

            // Time format must be valid
            if (viewModel.CorrectTimeFormat() == false)
            {
                ModelState.AddModelError("Time", "The time format is invalid.");

                // Events list is not null
                viewModel.EventTypes = _context.EventTypes.ToList();

                return(View("EventForm", viewModel));
            }

            // Ensure form validation
            if (!ModelState.IsValid || viewModel.DateTimeAvailable() == false)
            {
                ModelState.AddModelError("Date", "The date/time is not available.");
                ModelState.AddModelError("Time", "The date/time is not available.");

                // Events list is not null
                viewModel.EventTypes = _context.EventTypes.ToList();

                return(View("EventForm", viewModel));
            }

            var userId = User.Identity.GetUserId();

            // Get the event to change
            var eVent = _context.Events.Single(e => e.Id == viewModel.Id && e.EventOwnerId == userId);

            // Event properties are updated
            eVent.EventName     = viewModel.EventName;
            eVent.EventLocation = viewModel.EventLocation;
            eVent.DateTime      = viewModel.GetDateTime();
            eVent.Description   = viewModel.Description;
            eVent.EventTypeId   = viewModel.EventType;

            // Update database
            _context.SaveChanges();

            return(RedirectToAction("MyEvents", "Events"));
        }
Пример #5
0
        public ActionResult Edit(int eventId)
        {
            var userId = User.Identity.GetUserId();
            var eVent  = _context.Events.Single(e => e.Id == eventId && e.EventOwnerId == userId);

            var viewModel = new EventsFormViewModel
            {
                Id            = eVent.Id,
                Heading       = "Edit an Event",
                EventTypes    = _context.EventTypes.ToList(),
                EventName     = eVent.EventName,
                Date          = eVent.DateTime.ToString("d MMM yyyy"),
                Time          = eVent.DateTime.ToString("HH:mm"),
                EventLocation = eVent.EventLocation,
                Description   = eVent.Description,
                EventType     = eVent.EventTypeId
            };

            return(View("EventForm", viewModel));
        }