public async Task <IActionResult> EditMealCategory(CreateMealCategoryViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (Request.Form.Files.Any())
                    {
                        var file = model.CategoryImage;
                        if (file != null && file.Length > 0)
                        {
                            string uri = ImageSave(file, "categoryImage");
                            model.ImageUrl = uri;
                        }
                    }
                    await mealApi.UpdateMealCategory(model);

                    return(RedirectToAction("Categories"));
                }
            }
            catch (Exception ex)
            {
                // Log error here
                ModelState.AddModelError("", "An unknown error occured!");
            }

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task UpdateMealCategory(CreateMealCategoryViewModel data)
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseUrl);
                var httpContent         = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
                HttpResponseMessage msg = await client.PutAsync($"meals/category/{data.Id}", httpContent);

                msg.EnsureSuccessStatusCode();
            }
        }
        public async Task <IActionResult> EditMealCategory(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }
            var mealCategory = await mealApi.GetMealCategoryById(id.Value);

            if (mealCategory == null)
            {
                return(NotFound());
            }
            var model = new CreateMealCategoryViewModel
            {
                Id       = mealCategory.Id,
                Name     = mealCategory.Name,
                Priority = mealCategory.Priority
            };

            return(View(model));
        }