public async Task <IActionResult> Create(CategoryAddVM model) { if (ModelState.IsValid) { var serverPath = _env.ContentRootPath; //Directory.GetCurrentDirectory(); //_env.WebRootPath; var folerName = "Uploads"; var path = Path.Combine(serverPath, folerName); // if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string ext = Path.GetExtension(model.Image.FileName); string fileName = Path.GetRandomFileName() + ext; string filePathSave = Path.Combine(path, fileName); // сохраняем файл в папку Files в каталоге wwwroot using (var fileStream = new FileStream(filePathSave, FileMode.Create)) { await model.Image.CopyToAsync(fileStream); } Category category = new Category() { Name = model.Name, Image = fileName, UrlSlug = model.UrlSlug, DateCreate = DateTime.Now }; _context.Categories.Add(category); _context.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); }
public ActionResult Create(CategoryAddVM model) { //ModelState.AddModelError("", "Вас взломали :)!"); //ModelState.AddModelError("", "Ха ха ха!"); //return View(model); if (model.ImageFile == null) { ModelState.AddModelError("ImageFile", "Оберіть фото для тварини!"); return(View(model)); } var list = repo.GetAll(x => x.Name == model.Name).ToList(); if (list.Count > 0) { ModelState.AddModelError("Name", "Тварина з даним ім'ям уже є!"); return(View(model)); } if (ModelState.IsValid) { string fileName = Path.GetRandomFileName() + ".jpg"; string serverPath = Server.MapPath("~/Uploading"); string fileSave = Path.Combine(serverPath, fileName); model.ImageFile.SaveAs(fileSave); Category animal = new Category { Name = model.Name, Image = fileName, CreateDate = DateTime.Now, ModifyDate = DateTime.Now, DeleteDate = DateTime.Now }; repo.Create(animal); repo.Save(); //context.Animals.Add(animal); //context.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); }
public IActionResult Category(CategoryAddVM category) { if (!ModelState.IsValid) { return(View(category)); } if (!(PService.CheckExistCategoryEName(category.EName) || PService.CheckExistCategoryName(category.Name))) { PService.AddCategory(new Category { EName = category.EName, IsActive = category.IsActive, Name = category.Name, OrderNumber = category.OrderNumber, Description = category.Description, IsFavorate = category.IsFavorate, MainCategoryId = category.MainCategoryId }); } return(View(category)); }