Exemplo n.º 1
0
        public ActionResult Create(Teacher teacher)
        {
            if (ModelState.IsValid)
            {
                db.Teachers.Add(teacher);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(teacher);
        }
Exemplo n.º 2
0
        public ActionResult Create(HttpPostedFileBase file, Teacher teacher)
        {
            if (ModelState.IsValid)
            {
                if (file == null)
                {
                    ModelState.AddModelError("File", "Please Upload Your file");
                }
                else if (file.ContentLength > 0)
                {
                    int MaxContentLength = 1024 * 1024 * 3; //3 MB
                    string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf" };

                    if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
                    {
                        ModelState.AddModelError("File", "Please file of type: " + string.Join(", ", AllowedFileExtensions));
                    }

                    else if (file.ContentLength > MaxContentLength)
                    {
                        ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
                    }
                    else
                    {
                        //TO:DO
                        var sGuid = Guid.NewGuid().ToString();
                        var fileName = sGuid + file.FileName.Substring(file.FileName.LastIndexOf('.'));

                        var path = Path.Combine(Server.MapPath("~/Upload/TeacherImages/"), fileName);
                        file.SaveAs(path);
                        ModelState.Clear();
                        ViewBag.Message = "File uploaded successfully";
                        teacher.Photo = "~/Upload/TeacherImages/" + fileName;

                    }

                    if (ModelState.IsValid)
                    {
                        db.Teachers.Add(teacher);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }

                }

            }
            return View(teacher);
        }
Exemplo n.º 3
0
 public ActionResult Edit(Teacher teacher)
 {
     if (ModelState.IsValid)
     {
         db.Entry(teacher).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(teacher);
 }