public ActionResult Create(CreateSessionViewModel newSession)
        {
            using (WSADDbContext context = new WSADDbContext())
            {
                Session newSessionDTO = new WSAD_App1.Models.Data.Session()
                {
                    Title       = newSession.Title,
                    Description = newSession.Description,
                    Presenter   = newSession.Presenter,
                    Room        = newSession.Room,
                    Time        = newSession.Time,
                    Occupancy   = newSession.Occupancy
                };

                newSessionDTO = context.Sessions.Add(newSessionDTO);

                context.SaveChanges();
            }

            return(RedirectToAction("index"));
        }
Esempio n. 2
0
        public ActionResult Create(CreateSessionViewModel newSession)
        {
            //Check required fields
            if (!ModelState.IsValid)
            {
                return(View(newSession));
            }

            //Create DbContext
            using (WSADDbContext context = new WSADDbContext())
            {
                //Check for duplicate sessions
                if (context.Sessions.Any(row => row.Course.Equals(newSession.Course)))
                {
                    ModelState.AddModelError("", "Session '" + newSession.Course + "' already exists. Try Again");
                    newSession.Course = "";
                    return(View(newSession));
                }

                //Create Session DTO
                Session newSessionDTO = new WSAD_App1.Models.Data.Session()
                {
                    Course      = newSession.Course,
                    Instructor  = newSession.Instructor,
                    MeetingDate = newSession.MeetingDate,
                    MeetingTime = newSession.MeetingTime,
                    Description = newSession.Description
                };

                //Add to DbContext

                newSessionDTO = context.Sessions.Add(newSessionDTO);

                //Save changes
                context.SaveChanges();
            }

            //Redirect to login
            return(RedirectToAction("Index"));
        }