public ActionResult DeleteConfirmed(int id)
        {
            CurrentResearch currentResearch = db.CurrentResearches.Find(id);

            // get the filename, if one exists
            if (currentResearch.Image != null)
            {
                var fileName = Path.GetFileName(currentResearch.Image);

                // get the path to the file
                var path = Path.Combine(Server.MapPath("~/Images/CurrentResearch"), fileName);

                // delete the file
                try
                {
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                }
                catch (Exception)
                {
                    ModelState.AddModelError("uploadError", "Can't delete the file.");
                }
            }

            db.CurrentResearches.Remove(currentResearch);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
예제 #2
0
        private void SetResearchViews()
        {
            ResearchQueue.Items.Clear();
            if (_viewModel.ProjectQueue.Count > 0)
            {
                CurrentResearch.SetViewModel(_viewModel.ProjectQueue[0]);

                foreach (var item in _viewModel.ProjectQueue.Skip(1))
                {
                    ResearchQueue.Items.Add(new ScientistResearchView(item));
                }
            }
        }
        // GET: /CurrentResearch/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CurrentResearch currentResearch = db.CurrentResearches.Find(id);

            if (currentResearch == null)
            {
                return(HttpNotFound());
            }
            return(View(currentResearch));
        }
        public ActionResult Create([Bind(Include = "ID,Title,Description,Image,DisplayOrder")] CurrentResearch currentResearch, HttpPostedFileBase file)
        {
            if (file != null)
            {
                // if file's content length is zero or no files submitted
                if (Request.Files.Count != 1 || Request.Files[0].ContentLength == 0)
                {
                    ModelState.AddModelError("uploadError", "Please select an image to upload.");
                    return(View(currentResearch));
                }

                // check the file size (max 4 Mb)
                if (Request.Files[0].ContentLength > 1024 * 1024 * 4)
                {
                    ModelState.AddModelError("uploadError", "File size can't exceed 4 MB");
                    return(View(currentResearch));
                }

                // check file extension
                string extension = Path.GetExtension(Request.Files[0].FileName).ToLower();

                if (extension != ".jpg" && extension != ".jpeg" && extension != ".gif" && extension != ".png")
                {
                    ModelState.AddModelError("uploadError", "Supported file extensions: jpg, jpeg, gif, png");
                    return(View(currentResearch));
                }

                // extract only the filename
                var fileName = Path.GetFileName(file.FileName);

                // store the file inside ~/Images folder
                var path = Path.Combine(Server.MapPath("~/Images/CurrentResearch"), fileName);

                try
                {
                    if (!System.IO.File.Exists(path))
                    {
                        Request.Files[0].SaveAs(path);
                        //System.IO.File.Delete(path);
                    }
                    else
                    {
                        ModelState.AddModelError("uploadError", "An image with that name already exists.  Please rename your image.");
                        return(View(currentResearch));
                    }
                }
                catch (Exception)
                {
                    ModelState.AddModelError("uploadError", "Can't save file to disk");
                }
            }


            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    currentResearch.Image = file.FileName;
                }
                else
                {
                    currentResearch.Image = null;
                }


                db.CurrentResearches.Add(currentResearch);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(currentResearch));
        }