Esempio n. 1
0
        public ActionResult Edit(course cou, HttpPostedFileBase photo)
        {
            TeacherDao teDao  = new TeacherDao();
            CourseDao  couDao = new CourseDao();

            if (ModelState.IsValid)
            {
                try
                {
                    if (photo != null && photo.ContentLength > 0)
                    {
                        course exists_cou = couDao.GetById(cou.co_id);
                        string image      = String.Concat(cou.co_name, photo.FileName);
                        if (!image.Equals(exists_cou.co_image))
                        {
                            var image_old = Path.Combine(Server.MapPath("~/Areas/Admins/Content/Photo/course/"),
                                                         System.IO.Path.GetFileName(exists_cou.co_image));
                            if (System.IO.File.Exists(image_old))
                            {
                                System.IO.File.Delete(image_old);
                            }
                            var image_new = Path.Combine(Server.MapPath("~/Areas/Admins/Content/Photo/course/"),
                                                         System.IO.Path.GetFileName(image));


                            photo.SaveAs(image_new);
                        }
                        cou.co_image      = image;
                        cou.co_updated_at = DateTime.Now;
                        couDao.Update(cou);
                    }
                    else
                    {
                        course exists_cou = couDao.GetById(cou.co_id);
                        cou.co_image = exists_cou.co_image;
                        couDao.Update(cou);
                    }
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(View("Error"));
                }
            }
            else
            {
                ViewBag.lst_te = teDao.GetAll();
                var        teacher = teDao.GetAll();
                SelectList list_te = new SelectList(teacher, "te_id", "te_name");
                ViewBag.list_te = list_te;
                return(View(cou));
            }
        }
Esempio n. 2
0
        public ActionResult Create(course cou, HttpPostedFileBase photo)
        {
            TeacherDao teDao = new TeacherDao();
            CourseDao  dao   = new CourseDao();

            if (ModelState.IsValid)
            {
                try
                {
                    if (photo != null && photo.ContentLength > 0)
                    {
                        string image = String.Concat(photo.FileName);
                        var    path  = Path.Combine(Server.MapPath("~/Areas/Admins/Content/Photo/Course/"),
                                                    System.IO.Path.GetFileName(image));
                        photo.SaveAs(path);

                        cou.co_image = image;

                        cou.co_created_at = DateTime.Now;

                        //var filename = Path.GetFileName(photo.FileName);
                        //var path = Path.Combine(Server.MapPath("~/Areas/Admins/Content/Photo/Course/"), filename);
                        //photo.SaveAs(path);
                        //// Add avatar reference to model and save
                        //cou.co_image = string.Concat("~/Areas/Admins/Content/Photo/Course/", filename);

                        dao.Create(cou);
                    }
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(View("Error"));
                }
            }
            else
            {
                ViewBag.lst_te = teDao.GetAll();
                DBIeltsContext db      = new DBIeltsContext();
                var            teacher = db.teachers.ToList();
                SelectList     listTe  = new SelectList(teacher, "te_id", "te_name");
                ViewBag.list_te = listTe;
                return(View(cou));
            }
        }
Esempio n. 3
0
        public ActionResult Edit(int?id)
        {
            CourseDao couDao = new CourseDao();

            if (id == null)
            {
                return(View("Error"));
            }
            course course = couDao.GetById(id);

            if (course == null)
            {
                return(HttpNotFound());
            }
            TeacherDao teDao   = new TeacherDao();
            var        teacher = teDao.GetAll();
            SelectList list_te = new SelectList(teacher, "te_id", "te_name");

            ViewBag.list_te = list_te;
            return(View(course));
        }