Esempio n. 1
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));
            }
        }