public IActionResult Add(HamperAddViewModel VM) { if (ModelState.IsValid) { string hamperName = _textFormatter. RemoveDoubleSpaces(_textFormatter. CapitaliseFirstLetters(VM.HamperName, false)); string description = VM.Description != null && VM.Description != "" ? _textFormatter. RemoveDoubleSpaces(VM.Description) : ""; Hamper newHamper = new Hamper() { HamperName = hamperName, Description = description, Price = VM.Price, Products = VM.Products, ImageUrl = VM.ImageUrl, Supplier = VM.Supplier, InUse = true, CategoryId = VM.CategoryId }; _hamperService.Create(newHamper); return(RedirectToAction("Edit", "Category", new { CategoryId = newHamper.CategoryId })); } else { return(View(VM)); } }
public IActionResult AddHamper(string hamperId) { HamperAddViewModel vm = new HamperAddViewModel(); IEnumerable <Category> categoryList = _categoryDataService.GetAll(); vm.Categories = categoryList.Select(c => new HamperCategoryViewModel { CategoryId = c.CategoryId, CategoryName = c.CategoryName }).ToList(); int hId; if (int.TryParse(hamperId, out hId)) { Hamper hamper = _hamperDataService.GetSingle(h => h.HamperId == hId); if (hamper != null) { vm.HamperId = hamper.HamperId; vm.CategoryId = hamper.CategoryId; vm.Image = hamper.Image; vm.Name = hamper.Name; vm.Price = hamper.Price; vm.Details = hamper.Details; vm.Discontinue = hamper.Discontinue; } } return(View(vm)); }
public IActionResult Add(int id) { Category cat = _catDataService.GetSingle(c => c.CategoryId == id); HamperAddViewModel vm = new HamperAddViewModel { CategoryId = cat.CategoryId, CategoryName = cat.CategoryName }; return(View(vm)); }
public async Task <IActionResult> AddHamper(HamperAddViewModel vm, IFormFile file) { if (ModelState.IsValid) { Hamper hamper = new Hamper { HamperId = vm.HamperId, CategoryId = vm.CategoryId, Name = vm.Name, Price = vm.Price, Details = vm.Details, Discontinue = vm.Discontinue, Image = vm.Image, Rate = 0 }; if (hamper.HamperId == 0) { _hamperDataService.Create(hamper); } if (file != null) { var uploadPath = Path.Combine(_environmet.WebRootPath, "uploads"); string extention = Path.GetExtension(file.FileName); string fileName = hamper.HamperId + extention; using (var fileStream = new FileStream(Path.Combine(uploadPath, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); } hamper.Image = fileName; } _hamperDataService.Update(hamper); } IEnumerable <Category> categoryList = _categoryDataService.GetAll(); vm.Categories = categoryList.Select(c => new HamperCategoryViewModel { CategoryId = c.CategoryId, CategoryName = c.CategoryName }).ToList(); return(View(vm)); }
public async Task <IActionResult> Add(HamperAddViewModel vm, IFormFile upload) { if (ModelState.IsValid) { if (upload == null) { Hamper hamper = new Hamper { CategoryId = vm.CategoryId, HamperName = vm.HamperName, Details = vm.Details, Price = vm.Price }; _context.TblHamper.Add(hamper); } else { // Create a binary read to read the data from the upload file BinaryReader binaryReader = new BinaryReader(upload.OpenReadStream()); // Read the bytes, specifying the length of the content byte[] fileData = binaryReader.ReadBytes((int)upload.Length); // Extract the file name; we don't want the whole path var fileName = Path.GetFileName(upload.FileName); Hamper hamper = new Hamper { CategoryId = vm.CategoryId, HamperName = vm.HamperName, Details = vm.Details, Price = vm.Price, FileName = fileName, ContentSize = upload.Length, ContentType = upload.ContentType, FileContent = fileData }; //_dataService.Create(hamper); _context.TblHamper.Add(hamper); } await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Category")); } //if invalid return(View(vm)); }
public IActionResult Add(int CategoryId) { if (_catService.GetAll().Count() > 0) { HamperAddViewModel VM = new HamperAddViewModel { CategoryId = CategoryId }; IEnumerable <Category> categories = _catService.GetAll(); ViewBag.CategoriesExist = true; ViewBag.Categories = categories; return(View(VM)); } else { ViewBag.CategoriesExist = false; return(View()); } //TempData["categoryId"] = CategoryId.ToString(); //_httpContextAccessor.HttpContext.Session.SetString("categoryId", CategoryId.ToString()); }