Пример #1
0
 public bool Delete(int id)
 {
     try
     {
         var user = db.Students.Find(id);
         db.Students.Remove(user);
         db.SaveChanges();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Пример #2
0
        public ActionResult Add(Student model, string submit)
        {
            if (submit == "Thêm")
            {
                isThemMoi = true;
                if (model != null)
                {
                    model.Name_Student = model.Name_Student.ToString().Trim() ?? "";
                    model.DateOfBirth  = model.DateOfBirth;
                    model.Address      = model.Address.ToString().Trim() ?? "";
                    model.Email        = model.Email ?? "";
                    model.Phone        = model.Phone.ToString().Trim() ?? "";
                    model.ClassID      = model.ClassID;
                    model.CreatedDate  = model.CreatedDate.GetValueOrDefault(System.DateTime.Now);


                    db.Students.Add(model);
                    db.SaveChanges();
                    model = null;
                }
                SetAlert("Thêm thông tin thành công! :D", "success");
                return(RedirectToAction("Index"));
            }
            else if (submit == "Cập Nhật")
            {
                isThemMoi = false;
                if (model != null)
                {
                    var list = db.Students.SingleOrDefault(x => x.ID == model.ID);
                    list.Name_Student = model.Name_Student.ToString().Trim();
                    list.DateOfBirth  = model.DateOfBirth;
                    list.Address      = model.Address.ToString().Trim();
                    list.Email        = model.Email;
                    list.Phone        = model.Phone.ToString().Trim();
                    list.ClassID      = model.ClassID;
                    list.ModifiedDate = model.CreatedDate.GetValueOrDefault(System.DateTime.Now);
                    db.SaveChanges();
                    model = null;
                }
                SetAlert("Cập nhật thông tin thành công! :D", "success");
                return(RedirectToAction("Index"));
            }
            else if (submit == "Tìm")
            {
                if (!string.IsNullOrEmpty(model.Name_Student))
                {
                    List <Student> list = GetData().Where(s => s.Name_Student.Contains(model.Name_Student)).ToList();
                    return(View("Index", list));
                }
                else
                {
                    List <Student> list = GetData();
                    return(View("Index", list));
                }
            }
            else
            {
                List <Student> list = GetData().OrderBy(s => s.Name_Student).ToList();
                return(View("Index", list));
            }
        }