public async Task <IActionResult> Create(TableCreateVM objTable) { if (objTable.Table.SeatingName == null) { ModelState.AddModelError("Table.SeatingName", "Please enter a number"); return(View(objTable)); } if (ModelState.IsValid) { string TableName = objTable.Table.SeatingName + " Seater"; var DoesTableAlreadyExist = _db.Table.Where(t => t.SeatingName == TableName); if (DoesTableAlreadyExist.Any()) { StatusMessage = "Error : A " + TableName + " Table already exists."; } else { objTable.Table.SeatingName += " Seater"; _db.Table.Add(objTable.Table); await _db.SaveChangesAsync(); // BEGINNING OF IMAGE //Image saving Section //Name of image will be the ID of the menuItem number //Extracting the root path for where images will be saved string webRootPath = _hostingEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; //Recieving MenuItem From DB var tableFromDb = await _db.Table.FindAsync(objTable.Table.Id); if (files.Count > 0) { //File was upload //Combining the webroot path with images var uploads = Path.Combine(webRootPath, @"images\Table"); var extension = Path.GetExtension(files[0].FileName); using (var filesStream = new FileStream(Path.Combine(uploads, objTable.Table.Id + extension), FileMode.Create)) { //will copy the image to the file location on the sever and rename it files[0].CopyTo(filesStream); } //In database the name will be changed to te loctaion where the image is saved tableFromDb.Image = @"\images\Table\" + objTable.Table.Id + extension; } else { //No file uploaded so default will be used var uploads = Path.Combine(webRootPath, @"images\Table\" + SD.DefaultTableImage); System.IO.File.Copy(uploads, webRootPath + @"\images\Table\" + objTable.Table.Id + ".png"); tableFromDb.Image = @"\images\Table\" + objTable.Table.Id + ".png"; } await _db.SaveChangesAsync(); //END of IMAGE return(RedirectToAction(nameof(Index))); } } objTable.StatusMessage = StatusMessage; return(View(objTable)); }
//Get - Create public IActionResult Create() { TableCreateVM objT = new TableCreateVM(); return(View(objT)); }