コード例 #1
0
        public async Task <IActionResult> Create(RecipeViewModel model)//[Bind("RecipePicture,RecipeId,RecipeName,DishType,RecipeDescription,Ingredients,PreCookingPreparationMode,CookingPreparationMode,PostCookingPreparationMode,FoodAllergies,Symptoms,Antidote,Author")] Recipe recipe)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = UploadedFile(model);

                Recipe recipe = new Recipe
                {
                    RecipePicture              = uniqueFileName,
                    RecipeId                   = model.RecipeId,
                    RecipeName                 = model.RecipeName,
                    DishType                   = model.DishType,
                    RecipeDescription          = model.RecipeDescription,
                    Ingredients                = model.Ingredients,
                    PreCookingPreparationMode  = model.PreCookingPreparationMode,
                    CookingPreparationMode     = model.CookingPreparationMode,
                    PostCookingPreparationMode = model.PostCookingPreparationMode,
                    FoodAllergies              = model.FoodAllergies,
                    Author = User.Identity.Name,
                };
                _context.Add(recipe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }
コード例 #2
0
        public async Task <IActionResult> Create(RecipeViewModel model) //[Bind("RecipePicture,RecipeId,RecipeName,DishType,RecipeDescription,Ingredients,PreCookingPreparationMode,CookingPreparationMode,PostCookingPreparationMode,FoodAllergies,Symptoms,Antidote,Author")] Recipe recipe)
        {
            if (ModelState.IsValid)                                     //397b8088-e7e9-45dc-937a-4dc2d471e109_default_image_placeholder.jpg
            {
                //string uniqueFileName = UploadedFile(model);
                string uniqueFileName = null;
                if (uniqueFileName != null)
                {
                    // Handles uploading of new images
                    uniqueFileName = UploadedFile(model);
                }
                else
                {
                    // Handles retaining old images if user wishes not to change the initial image
                    uniqueFileName = UploadedPlaceholderFile(model);
                }

                Recipe recipe = new Recipe
                {
                    RecipePicture              = uniqueFileName,
                    RecipeId                   = model.RecipeId,
                    RecipeName                 = model.RecipeName,
                    DishType                   = model.DishType,
                    RecipeDescription          = model.RecipeDescription,
                    Ingredients                = model.Ingredients,
                    PreCookingPreparationMode  = model.PreCookingPreparationMode,
                    CookingPreparationMode     = model.CookingPreparationMode,
                    PostCookingPreparationMode = model.PostCookingPreparationMode,
                    FoodAllergies              = model.FoodAllergies,
                    Author = User.Identity.Name,
                };
                _context.Add(recipe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }