Пример #1
0
        public ActionResult Add(AddRecipeViewModel recipe)
        {
            var validTypes = new[] { "image/jpeg", "image/pjpeg", "image/png", "image/gif" };

            if (recipe.PhotoUpload != null && !validTypes.Contains(recipe.PhotoUpload.ContentType))
            {
                ModelState.AddModelError("PhotoUpload", "Please upload either a JPG, GIF, or PNG image.");
            }

            if (!ModelState.IsValid)
            {
                IEnumerable <SelectItemViewModel> categoriesList = new List <SelectItemViewModel>();
                categoriesList = _recipeProvider.GetSelectCategories();


                if (recipe.PhotoUpload.ContentLength > 0)
                {
                    // A file was uploaded
                    var    fileName   = Path.GetFileName(recipe.PhotoUpload.FileName);
                    string uploadPath = "~/Images/Recipe/Big/";
                    var    path       = Path.Combine(Server.MapPath(uploadPath));
                    recipe.PhotoUpload.SaveAs(path);
                    recipe.RecipeImage = uploadPath + fileName;
                }


                var viewModel = new AddRecipeViewModel
                {
                    RecipeName        = recipe.RecipeName,
                    RecipeImage       = recipe.RecipeImage,
                    PhotoUpload       = recipe.PhotoUpload,
                    RecipeDescription = recipe.RecipeDescription,
                    CreatedAt         = recipe.CreatedAt,
                    ModefiedAt        = recipe.ModefiedAt,
                    CookingTime       = recipe.CookingTime,
                    RecipeCategoryId  = recipe.RecipeCategoryId,
                    Categories        = categoriesList
                };

                return(View("Add", viewModel));
            }

            if (recipe.PhotoUpload != null && recipe.PhotoUpload.ContentLength > 0)
            {
                // A file was uploaded
                var    fileName   = Path.GetFileName(recipe.PhotoUpload.FileName);
                string uploadPath = "~/Images/Recipe/Big/";
                var    path       = Path.Combine(Server.MapPath(uploadPath), fileName);
                recipe.PhotoUpload.SaveAs(path);
                recipe.RecipeImage = uploadPath + fileName;
            }
            else
            {
                recipe.RecipeImage = "/Images/Recipe/Big/noimage.JPEG";
            }
            _recipeProvider.AddRecipe(recipe);
            return(RedirectToAction("Index"));
        }
Пример #2
0
        public IActionResult Create(RecipeViewModel recipeViewModel)
        {
            var recipe = new Recipe();

            recipe.Name        = recipeViewModel.Name;
            recipe.Ingredients = new List <RecipeIngredient>();

            recipe = _recipeProvider.AddRecipe(recipe);

            return(RedirectToAction(nameof(Details), new { id = recipe.ID }));
        }