public async Task <IActionResult> Create(ProgrameSession programeSession)
        {
            var user = await GetCurrentUserAsync();

            programeSession.ApplicationUserId = user?.Id;
            if (ModelState.IsValid)
            {
                var sessionname  = _context.Sessions.Where(s => s.SessionId == programeSession.SessionId).SingleOrDefault();
                var programename = _context.Programes.Where(s => s.ProgrameId == programeSession.ProgrameId).SingleOrDefault();
                if (sessionname != null)
                {
                    var details = "(" + sessionname.Name + "-" + (programename != null ? programename.Name : "") + ")" + programeSession.Details;
                    var model   = _context.ProgrameSessions.Where(s => s.Details.Trim() == details.Trim()).FirstOrDefault();
                    if (model != null)
                    {
                        ViewData["SessionId"]  = new SelectList(_context.Sessions, "SessionId", "Name");
                        ViewData["ProgrameId"] = new SelectList(_context.Programes, "ProgrameId", "Name");
                        ModelState.AddModelError(string.Empty, "Program-Session already exists.Please select another combination of Program-Session.");
                        return(View(programeSession));
                    }
                    programeSession.Details = details;
                }
                _context.Add(programeSession);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SessionId"]  = new SelectList(_context.Sessions, "SessionId", "Name");
            ViewData["ProgrameId"] = new SelectList(_context.Programes, "ProgrameId", "Name");
            return(View(programeSession));
        }
コード例 #2
0
 public void UpdateProgrameSession(ProgrameSession entity, ProgrameSession model)
 {
     //entity.UserId = model.UserId;
     entity.SessionId   = model.SessionId;
     entity.ProgrameId  = model.ProgrameId;
     entity.Details     = model.Details;
     entity.RegDate     = model.RegDate;
     entity.Description = model.Description;
 }
        public async Task <IActionResult> Edit(int id, ProgrameSession programeSession)
        {
            var user = await GetCurrentUserAsync();

            programeSession.ApplicationUserId = user?.Id;
            if (id != programeSession.ProgrameSessionId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                var sessionname  = _context.Sessions.Where(s => s.SessionId == programeSession.SessionId).SingleOrDefault();
                var programename = _context.Programes.Where(s => s.ProgrameId == programeSession.ProgrameId).SingleOrDefault();
                if (sessionname != null)
                {
                    var details = "(" + sessionname.Name + "-" + (programename != null ? programename.Name : "") + ")" + programeSession.Details;
                    var model   = _context.ProgrameSessions.Where(s => s.Details.Trim() == details.Trim() && s.ProgrameSessionId != id).ToList();
                    if (model.Count != 0)
                    {
                        ViewData["SessionId"]  = new SelectList(_context.Sessions, "SessionId", "Name");
                        ViewData["ProgrameId"] = new SelectList(_context.Programes, "ProgrameId", "Name");
                        ModelState.AddModelError(string.Empty, "Program-Session already exists.Please select another combination of Program-Session.");
                        return(View(programeSession));
                    }
                    programeSession.Details = details;
                }
                try
                {
                    _context.Update(programeSession);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProgrameSessionExists(programeSession.ProgrameSessionId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["SessionId"]  = new SelectList(_context.Sessions, "SessionId", "Name");
            ViewData["ProgrameId"] = new SelectList(_context.Programes, "ProgrameId", "Name");
            return(View(programeSession));
        }