// GET: Recipe/Edit/5 public ActionResult Edit(int?id) { if (!m.CanUserEdit(id.GetValueOrDefault())) { return(RedirectToAction("Index")); } if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Recipe_IngredViewModel recipe = m.mapper.Map <RecipeWithIngredBaseViewModel, Recipe_IngredViewModel>(m.RecipeWithIngredGetById(id)); IEnumerable <IngredientBaseViewModel> ingredients = m.IngredientGetAll(); String[] selectedIngreds = m.ingredsForRecipe(id).ToArray(); IEnumerable <DietDescViewModel> diets = m.DietGetAll(); String[] selectedDiets = m.dietsForRecipe(id).ToArray(); if (recipe == null) { return(HttpNotFound()); } recipe.ingredients = ingredients; recipe.selectedIngredsId = selectedIngreds; recipe.diets = diets; recipe.selectedDietsId = selectedDiets; if (recipe.Content != null && recipe.Content_Type != null) { string base64 = Convert.ToBase64String(recipe.Content); recipe.fileResult = String.Format("data:{0};base64,{1}", recipe.Content_Type, base64); } return(View(recipe)); }
public ActionResult Edit(Recipe_IngredViewModel recipes, HttpPostedFileBase file) { Recipe_IngredViewModel recipe = m.mapper.Map <RecipeWithIngredBaseViewModel, Recipe_IngredViewModel>(m.RecipeWithIngredGetById(recipes.recipe_Id)); IEnumerable <IngredientBaseViewModel> ingredients = m.IngredientGetAll(); String[] selectedIngreds = m.ingredsForRecipe(recipes.recipe_Id).ToArray(); IEnumerable <DietDescViewModel> diets = m.DietGetAll(); String[] selectedDiets = m.dietsForRecipe(recipes.recipe_Id).ToArray(); recipe.ingredients = ingredients; recipe.selectedIngredsId = selectedIngreds; recipe.diets = diets; recipe.selectedDietsId = selectedDiets; if (m.IsNoSpecial(recipes.title) == false || m.IsNoSpecial(recipes.country) == false || m.IsNoSpecial(recipes.mealTimeType) == false) { ModelState.AddModelError("", "No Special Characters Allowed"); return(View(recipe)); } if (recipe == null) { return(HttpNotFound()); } if (recipe.Content != null && recipe.Content_Type != null) { string base64 = Convert.ToBase64String(recipe.Content); recipe.fileResult = String.Format("data:{0};base64,{1}", recipe.Content_Type, base64); } if (!ModelState.IsValid) { ModelState.AddModelError("", "Error while submitting the form. Please check the values submitted"); return(View(recipe)); } try { //Check for Diet conflict bool compatDiet = true; if (recipes.selectedDietsId != null) { for (int i = 0; i < recipes.selectedDietsId.Length; i++) { if (recipes.selectedDietsId[i] == "10" && recipes.selectedDietsId.Length > 1) { compatDiet = false; } else if (recipes.selectedDietsId[i] == "8" || recipes.selectedDietsId[i] == "5") { for (int j = 0; j < recipes.selectedDietsId.Length; j++) { if (recipes.selectedDietsId[j] != "6" && recipes.selectedDietsId[j] != "5" && recipes.selectedDietsId[j] != "8") { compatDiet = false; } } } } if (compatDiet == false) { ModelState.AddModelError("", "Incompatable Diets Selected"); return(View(recipe)); } } if (file != null && file.ContentLength > 0) { if (file.ContentLength / 1024 > 50) { ModelState.AddModelError("", "Image size should be less than 50kb"); return(View(recipe)); } recipes.Content_Type = file.ContentType; using (var reader = new System.IO.BinaryReader(file.InputStream)) { recipes.Content = reader.ReadBytes(file.ContentLength); } } var editedrecipe = m.RecipeEdit(recipes); if (editedrecipe == null) { ModelState.AddModelError("", "Error while editing a recipe. Please try again"); return(View(recipe)); } else { return(RedirectToAction("Details", new { id = editedrecipe.recipe_Id })); } } catch { ModelState.AddModelError("", "Error while editing a recipe. Please try again"); return(View(recipe)); } }