/// <summary> /// Конструктор по умолчанию /// </summary> /// <param name="category">Категория</param> public CategoryModel(Category category) { id = category.Id; title = category.Title; img = category.Image; objType = "category"; }
public ActionResult Save(Category model) { var file = Request.Files["Image"]; string imageUrl = null; if (file != null && file.ContentLength > 0 && file.ContentType.ToLower().Contains("image")) { var fileName = Path.ChangeExtension(Path.GetRandomFileName(), ".jpg"); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Files", "Categories"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var fullPath = Path.Combine(path, fileName); ImageHelper.SaveAs(file, fullPath); imageUrl = "/Files/Categories/" + fileName; } if (model.Id <= 0) { model.DateCreated = DateTime.Now; model.Image = imageUrl; Repository.Add(model); Repository.SubmitChanges(); ShowSuccess("Категория успешно добавлена"); } else { // Ищем var cat = Repository.Load(model.Id); if (cat == null) { ShowError("Такая категория не найдена"); return RedirectToAction("Index"); } // Пытаемся обновить var oldImage = cat.Image; TryUpdateModel(cat); cat.DateModified = DateTime.Now; cat.Image = imageUrl ?? oldImage; Repository.SubmitChanges(); ShowSuccess("Категория успешно отредактирована"); } return RedirectToAction("Index"); }