コード例 #1
0
        public ContentResult AddAjax(EditRecipeViewModel editRecipe)
        {
            int status = 1;

            IEnumerable <SelectItemViewModel> categoriesList = new List <SelectItemViewModel>();

            categoriesList = _recipeProvider.GetSelectCategories();

            editRecipe.Categories = categoriesList;

            var image     = editRecipe.PhotoUpload;
            var imageSave = WorkWithImage.CreateImage(image, 500, 400);

            if (ModelState.IsValid)
            {
                var result = _recipeProvider.EditRecipe(editRecipe);

                if (result == 0)
                {
                    ModelState.AddModelError("", "Ошибка! Невозможно сохранить! Проверьте все ли поля указаны.");
                }
                else if (result != 0)
                {
                    if (editRecipe.RecipeImage != null && editRecipe.PhotoUpload == null)
                    {
                        _recipeProvider.EditRecipe(editRecipe);
                    }
                    else if (/*editRecipe.PhotoUpload.ContentLength > 0*/ editRecipe.PhotoUpload != null)
                    {
                        // A file was uploaded
                        var    fileName   = Path.GetFileName(editRecipe.PhotoUpload.FileName);
                        string uploadPath = "~/Images/Recipe/Big/";
                        var    path       = Path.Combine(Server.MapPath(uploadPath), fileName);
                        imageSave.Save(path, ImageFormat.Jpeg);
                        editRecipe.RecipeImage = uploadPath + fileName;
                    }
                    _recipeProvider.EditRecipe(editRecipe);
                }
            }

            ViewBag.ListProducts       = _recipeProvider.GetListItemProducts();
            ViewBag.ListMenus          = _recipeProvider.GetListItemMenus();
            ViewBag.ListProductWeights = _recipeProvider.GetListWeightProducts(editRecipe.Id);

            string json = JsonConvert.SerializeObject(new
            {
                result = status,
                recipe = editRecipe,
            });

            return(Content(json, "application/json"));
        }
コード例 #2
0
        public ActionResult Edit(EditRecipeViewModel editRecipe)
        {
            IEnumerable <SelectItemViewModel> categoriesList = new List <SelectItemViewModel>();

            categoriesList = _recipeProvider.GetSelectCategories();

            editRecipe.Categories = categoriesList;

            var image     = editRecipe.PhotoUpload;
            var imageSave = WorkWithImage.CreateImage(image, 500, 400);

            if (ModelState.IsValid)
            {
                var result = _recipeProvider.EditRecipe(editRecipe);

                if (result == 0)
                {
                    ModelState.AddModelError("", "Ошибка! Невозможно сохранить! Проверьте все ли поля указаны.");
                }
                else if (result != 0)
                {
                    if (editRecipe.RecipeImage != null && editRecipe.PhotoUpload == null)
                    {
                        _recipeProvider.EditRecipe(editRecipe);
                        return(RedirectToAction("Index")); /*RedirectToAction("ProductsWeight", new { id = editRecipe.Id })*/;
                    }
                    else if (/*editRecipe.PhotoUpload.ContentLength > 0*/ editRecipe.PhotoUpload != null)
                    {
                        // A file was uploaded
                        var    fileName   = Path.GetFileName(editRecipe.PhotoUpload.FileName);
                        string uploadPath = "~/Images/Recipe/Big/";
                        var    path       = Path.Combine(Server.MapPath(uploadPath), fileName);
                        imageSave.Save(path, ImageFormat.Jpeg);
                        editRecipe.RecipeImage = uploadPath + fileName;
                    }
                    _recipeProvider.EditRecipe(editRecipe);
                    return(RedirectToAction/*("ProductsWeight", new { id = editRecipe.Id })*/ ("Index"));
                }
            }

            ViewBag.ListProducts = _recipeProvider.GetListItemProducts();
            ViewBag.ListMenus    = _recipeProvider.GetListItemMenus();
            // ViewBag.ListProductWeights = _recipeProvider.GetListWeightProducts(editRecipe.Id);
            return(View(editRecipe));
        }