private void detach_Products(Product entity) { this.SendPropertyChanging(); entity.Place = null; }
public ActionResult SaveProduct(Product model, string nextAction = null) { 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", "Products"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var fullPath = Path.Combine(path, fileName); ImageHelper.SaveAs(file, fullPath); imageUrl = "/Files/Products/" + fileName; } if (model.Id <= 0) { model.DateCreated = DateTime.Now; model.Image = imageUrl; ProductsRepository.Add(model); ProductsRepository.SubmitChanges(); ShowSuccess("Продукт или товар успешно добавлен"); } else { // Ищем var product = ProductsRepository.Load(model.Id); if (product == null) { ShowError("Такой продукт не найден"); return RedirectToAction("Index"); } // Пытаемся обновить var oldImage = product.Image; TryUpdateModel(product); product.DateModified = DateTime.Now; product.Image = imageUrl ?? oldImage; Repository.SubmitChanges(); ShowSuccess("Товар или продукт успешно отредактирован"); } if (!String.IsNullOrEmpty(nextAction)) { return RedirectToAction("AddProduct", new {id = model.PlaceId, category = model.Category, productType = model.Type}); } return RedirectToAction("Products", new { id = model.PlaceId }); }
private void attach_Products(Product entity) { this.SendPropertyChanging(); entity.Place = this; }