Exemplo n.º 1
0
        public ActionResult New(Recipe r, HttpPostedFileBase recipeImage) //23. Add a parameter to support the image
        {
            ////TODO 13. Redirect to home page
            //return RedirectToAction("Index", "Home"); //--> Home/Index

            //TODO 18. Save the recipe to the database
            //      then redirect back to home
            //dal.SaveNewRecipe(r);
            //return RedirectToAction("Index", "Home");

            //TODO 24. Save the image the filesystem
            //      then save the recipe to the database
            if (recipeImage != null)
            {
                // Generate the final file path
                //string filename = Path.GetFileName(recipeImage.FileName);
                string extension     = Path.GetExtension(recipeImage.FileName);
                string filename      = Guid.NewGuid() + extension;
                string placeToSaveIt = Path.Combine(Server.MapPath("~/images/"), filename);

                // Save the image to the disk
                recipeImage.SaveAs(placeToSaveIt);
                r.ImageName = filename;
            }

            dal.SaveNewRecipe(r);

            //TODO 29. Reset CurrentRecipe to null
            Session["CurrentRecipe"] = null;

            return(RedirectToAction("Index", "Home"));
        }