public ActionResult AddOrEditRecipe(EditRecipeViewModel recipeViewModel) { if (recipeViewModel != null) { bool flag = false; if (recipeViewModel.ImageFile != null) { switch (recipeViewModel.ImageFile.ContentType) { case "image/gif": flag = false; break; case "image/jpeg": flag = false; break; case "image/png": flag = false; break; default: flag = true; break; } if (flag) { ModelState.AddModelError("ImageFile", "This field is for png/gif/jpg image"); } } } if (ModelState.IsValid) { string imagePath = @"D:\Images\"; if (!Directory.Exists(imagePath)) { Directory.CreateDirectory(imagePath); } if (recipeViewModel != null) { if (recipeViewModel.RecipeId == 0) { if (recipeViewModel.ImageFile != null) { imagePath += recipeViewModel.ImageFile.FileName; recipeViewModel.ImageFile.SaveAs(imagePath); recipeViewModel.ImageUrl = imagePath; } } else { Recipe oldRecipe = _recipeProvider.GetRecipeById(recipeViewModel.RecipeId); if (recipeViewModel.IsNullImage) { recipeViewModel.ImageFile = null; recipeViewModel.ImageUrl = null; } else { if (recipeViewModel.ImageFile != null) { imagePath += recipeViewModel.ImageFile.FileName; if (oldRecipe.ImageUrl != imagePath) { recipeViewModel.ImageFile.SaveAs(imagePath); recipeViewModel.ImageUrl = imagePath; } else { recipeViewModel.ImageUrl = oldRecipe.ImageUrl; } } } } } int recipeId = 0; Recipe recipe = ParseRecipe(recipeViewModel); RecipeDetails recipeDetails = ParseRecipeDetails(recipeViewModel); if (recipeViewModel.RecipeId == 0) { recipeId = _recipeProvider.InsertRecipe(recipe, recipeDetails); } else { _recipeProvider.UpdateRecipe(recipe, recipeDetails); recipeId = recipe.RecipeId; } if (recipeId != 0) { return(RedirectToAction("ShowFullRecipe", new { recipeId })); } else { return(RedirectToAction("ShowAllRecipes")); } } else { Recipe recipe = ParseRecipe(recipeViewModel); RecipeDetails recipeDetails = ParseRecipeDetails(recipeViewModel); recipeViewModel = ParseEditRecipeViewModel(recipe, recipeDetails); return(View("AddOrEditRecipe", recipeViewModel)); } }