/// <summary> /// получение обьекта по id/ /// </summary> /// <param name="id"></param> /// <returns></returns> public ModelAddObject GetMainLinq(int?id) { SchoolContext db = new SchoolContext("DefaultConnection"); var result = (from s in db.Students join Sh in db.Shedules on s.ClassId equals Sh.ClassId join t in db.Teachers on Sh.TeacherId equals t.Id join cl in db.Classes on Sh.ClassId equals cl.ClassId where s.Id == id select new { id = s.Id, ClassId = cl.ClassId, SheduleId = Sh.SheduleId, TeacherId = t.Id, FIOStud = s.FIO, BirthdayStud = s.Birtday, FIOteacher = t.FIOteacher, BirthdayTeach = t.Birthday, Exp = t.Exp, CountHour = t.CountHour, CountPeople = cl.CountPeople, NumberObject = Sh.NumberObject, Reiting = cl.Reiting }).ToList(); ModelAddObject model = new ModelAddObject(); foreach (var i in result) { model = new ModelAddObject() { Id = i.id, ClassId = i.ClassId, SheduleId = i.SheduleId, TeacherId = i.TeacherId, FIOStudent = i.FIOStud, BirtdayStud = i.BirthdayStud.ToShortDateString(), FIOteacher = i.FIOteacher, BirthdayTech = i.BirthdayTeach.ToShortDateString(), Exp = i.Exp, CountHour = i.CountHour, CountPeople = i.CountPeople, NumberObject = i.NumberObject, Reiting = i.Reiting }; } return(model); }
public ActionResult Delete(int?id) { try { SchoolContext db = new SchoolContext("DefaultConnection"); if (id == null) { return(HttpNotFound()); } ModelAddObject model = GetMainLinq(id); if (model == null) { return(HttpNotFound()); } Shedule shedule = db.Shedules.FirstOrDefault(s => s.SheduleId == model.SheduleId); db.Shedules.Remove(shedule); db.SaveChanges(); Student student = db.Students.FirstOrDefault(s => s.Id == model.Id); db.Students.Remove(student); db.SaveChanges(); //Teacher teacher = db.Teachers.FirstOrDefault(t => t.Id == model.TeacherId); //db.Teachers.Remove(teacher); //db.SaveChanges(); Class class1 = db.Classes.FirstOrDefault(t => t.ClassId == model.ClassId); db.Classes.Remove(class1); db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(HttpNotFound()); } }
public ActionResult Edit(int?id) { if (id == null) { return(HttpNotFound()); } _Id = id; SelectList teachers = new SelectList(db.Teachers, "Id", "FIOteacher"); ViewBag.ListT = teachers; ModelAddObject modelList = GetMainLinq(id); if (modelList != null) { return(View(modelList)); } else { return(HttpNotFound()); } }
public ActionResult Create([Bind(Include = "Id,ClassId,SheduleId,TeacherId,FIOStudent,BirtdayStud," + "BirthdayTech,FIOteacher,Exp,CountHour,CountPeople,Reiting,NumberObject")] ModelAddObject modelAdd) { SelectList teachers = new SelectList(db.Teachers, "Id", "FIOteacher"); ViewBag.ListT = teachers; DateTime dtstud; DateTime dtTeach; try { bool bSuccess = DateTime.TryParse(modelAdd.BirtdayStud, out dtstud); //bool bSuccess2 = DateTime.TryParse(modelAdd.BirthdayTech, out dtTeach); if (bSuccess == false) { ModelState.AddModelError("BirtdayStud", "Дата введена не верно"); } //if ( bSuccess2 == false) //{ // ModelState.AddModelError("BirthdayTech", "Дата введена не верно"); //} if (modelAdd.FIOStudent == null) { ModelState.AddModelError("FIOStudent", "ФИО студента должно быть обязательно заполнено"); } if (modelAdd.TeacherId == 0) { ModelState.AddModelError("FIOteacher", "ФИО учителя должно быть обязательно заполнено"); } //if (modelAdd.Exp == 0) //{ // ModelState.AddModelError("Exp", "Опыт учителя должнен быть обязательно заполнено"); //} //if (modelAdd.CountHour == 0) //{ // ModelState.AddModelError("CountHour", "Количество часов должно быть обязательно заполнено"); //} //if (modelAdd.CountPeople == 0) //{ // ModelState.AddModelError("CountPeople", "Количество человек в классе должно быть обязательно заполнено"); //} //if (modelAdd.Reiting == 0) //{ // ModelState.AddModelError("Reiting", "Рейтинг класса должен быть обязательно заполнен"); //} //if (modelAdd.NumberObject == "") //{ // ModelState.AddModelError("NumberObject", "Номер предмета должен быть обязательно заполнен"); //} if (ModelState.IsValid) { //SchoolContext db = new SchoolContext("DefaultConnection"); //Teacher teacher = new Teacher() //{ // FIOteacher = modelAdd.FIOteacher, // Birthday = Convert.ToDateTime(modelAdd.BirthdayTech), // Exp = modelAdd.Exp, // CountHour = modelAdd.CountHour //}; //db.Teachers.Add(teacher); //db.SaveChanges(); Class classadd = new Class() { CountPeople = modelAdd.CountPeople, Reiting = modelAdd.Reiting }; db.Classes.Add(classadd); db.SaveChanges(); Student studadd = new Student() { FIO = modelAdd.FIOStudent, Birtday = Convert.ToDateTime(modelAdd.BirtdayStud), ClassId = classadd.ClassId /*db.Classes.FirstOrDefault(c => c.CountPeople == modelAdd.CountPeople).ClassId*/, }; db.Students.Add(studadd); db.SaveChanges(); Shedule sheduleadd = new Shedule() { TeacherId = modelAdd.TeacherId /*db.Teachers.FirstOrDefault(t => t.FIOteacher == modelAdd.FIOteacher).Id*/, ClassId = classadd.ClassId /*db.Classes.FirstOrDefault(t => t.CountPeople == modelAdd.CountPeople).ClassId*/, NumberObject = modelAdd.NumberObject }; db.Shedules.Add(sheduleadd); db.SaveChanges(); return(RedirectToAction("Index")); } else { return(View(modelAdd)); } } catch { return(HttpNotFound()); } }
public ActionResult Edit([Bind(Include = "Id,ClassId,SheduleId,TeacherId,FIOStudent,BirtdayStud," + "BirthdayTech,FIOteacher,Exp,CountHour,CountPeople,Reiting,NumberObject")] ModelAddObject model) { try { SelectList teachers = new SelectList(db.Teachers, "Id", "FIOteacher"); ViewBag.ListT = teachers; DateTime dtstud; //DateTime dtTeach; bool bSuccess = DateTime.TryParse(model.BirtdayStud, out dtstud); //bool bSuccess2 = DateTime.TryParse(model.BirthdayTech, out dtTeach); if (bSuccess == false) { ModelState.AddModelError("BirtdayStud", "Дата введена не верно"); } //if (bSuccess2 == false) //{ // ModelState.AddModelError("BirthdayTech", "Дата введена не верно"); //} if (model.FIOStudent == null) { ModelState.AddModelError("FIOStudent", "ФИО студента должно быть обязательно заполнено"); } if (model.TeacherId == 0) { ModelState.AddModelError("FIOteacher", "ФИО учителя должно быть обязательно заполнено"); } //if (model.Exp == 0) //{ // ModelState.AddModelError("Exp", "Опыт учителя должнен быть обязательно заполнено"); //} //if (model.CountHour == 0) //{ // ModelState.AddModelError("CountHour", "Количество часов должно быть обязательно заполнено"); //} if (model.CountPeople == 0) { ModelState.AddModelError("CountPeople", "Количество человек в классе должно быть обязательно заполнено"); } if (model.Reiting == 0) { ModelState.AddModelError("Reiting", "Рейтинг класса должен быть обязательно заполнен"); } if (model.NumberObject == "") { ModelState.AddModelError("NumberObject", "Номер предмета должен быть обязательно заполнен"); } if (ModelState.IsValid) { //SchoolContext db = new SchoolContext("DefaultConnection"); //Teacher teacher = db.Teachers.FirstOrDefault(t => t.Id == model.TeacherId); //teacher.FIOteacher = model.FIOteacher; //teacher.Birthday = Convert.ToDateTime(model.BirthdayTech); //teacher.Exp = model.Exp; //teacher.CountHour = model.CountHour; //db.Entry(teacher).State = System.Data.Entity.EntityState.Modified; //db.SaveChanges(); Class class1 = db.Classes.FirstOrDefault(t => t.ClassId == model.ClassId); class1.CountPeople = model.CountPeople; class1.Reiting = model.Reiting; db.Entry(class1).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); Student student = db.Students.FirstOrDefault(s => s.Id == model.Id); student.FIO = model.FIOStudent; student.Birtday = Convert.ToDateTime(model.BirtdayStud); db.Entry(student).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); Shedule shedule = db.Shedules.FirstOrDefault(s => s.SheduleId == model.SheduleId); shedule.NumberObject = model.NumberObject; shedule.TeacherId = model.TeacherId; db.Entry(shedule).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } else { return(View(model)); } } catch { return(HttpNotFound()); } }