Example #1
0
 public void OnPost()
 {
     if (ModelState.IsValid)
     {
         _db.Messages.Add(new Message
         {
             Text = Text,
             When = DateTime.Now,
             Sign = User.Identity.Name
         });
         _db.SaveChanges();
     }
     Messages = _db.Messages.ToList();
 }
Example #2
0
        public IActionResult SelectAjax([FromBody] List <Testt> a)
        {
            if (ModelState.IsValid)
            {
                Student student = _context.Students.Include(s => s.DisciplineStudents).FirstOrDefault(s => s.Name == User.Identity.Name);

                for (int i = 0; i < a.Count; i++)
                {
                    Discipline discipline = _context.Disciplines
                                            .FirstOrDefault(c => c.Title == a[i].Title);

                    var studDis = student.DisciplineStudents.FirstOrDefault(sd => sd.DisciplineId == discipline.Id);

                    DisciplineStudent disciplineStudent = new DisciplineStudent()
                    {
                        DisciplineId = discipline.Id, StudentId = student.Id
                    };
                    if (a[i].IsChecked == true)
                    {
                        if (!student.DisciplineStudents.Contains(studDis))
                        {
                            student.DisciplineStudents.Add(disciplineStudent);
                        }
                    }
                    else
                    {
                        if (student.DisciplineStudents.Contains(studDis))
                        {
                            student.DisciplineStudents.Remove(studDis);
                        }
                    }
                    _context.SaveChanges();
                }
                Discipline.Clear();


                var students = _context.Students.Include(c => c.DisciplineStudents).ThenInclude(sc => sc.Discipline).ToList();
                SelectedDiscipline = students.FirstOrDefault(s => s.Id == student.Id).DisciplineStudents.Select(c => c.Discipline).ToList();

                var disciplines = _context.Disciplines.ToList().Except(SelectedDiscipline).ToList();
                NewDisceplines(SelectedDiscipline, true);
                NewDisceplines(disciplines, false);
                return(PartialView("~/Views/Home/SelectList.cshtml", Discipline));
            }
            return(PartialView("~/Views/Home/SelectList.cshtml", Discipline));
        }