//Get-Edit public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var table = await _db.Table.FindAsync(id); if (table == null) { return(NotFound()); } var integerValue = GetIntValue(table.SeatingName); if (integerValue == 0) { return(NotFound()); } table.SeatingName = integerValue.ToString(); TableEditVM objT = new TableEditVM { Table = table, OldMaxValue = table.MaxTables }; return(View(objT)); }
public async Task <IActionResult> Edit(TableEditVM objTable) { if (ModelState.IsValid) { if (objTable.Table.MaxTables != objTable.OldMaxValue) { int SubtractTab = objTable.Table.MaxTables - objTable.OldMaxValue; var TableTrackObj = await _db.TableTrack.Include(t => t.Table).Where(t => t.TableId == objTable.Table.Id).ToListAsync(); if (TableTrackObj.Any()) { foreach (var item in TableTrackObj) { TableTrack objTT = await _db.TableTrack.FindAsync(item.Id); objTT.AmtAva += SubtractTab; if (objTT.AmtAva < 0) { objTT.AmtAva = 0; } } } } var TableFromDb = await _db.Table.FindAsync(objTable.Table.Id); //Begin Image string webRootPath = _hostingEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; if (files.Count > 0) { //New Image File was upload //Combining the webroot path with images var uploads = Path.Combine(webRootPath, @"images\Table"); var extension_new = Path.GetExtension(files[0].FileName); // Delete Image File that was in Database var imagePath = Path.Combine(webRootPath, TableFromDb.Image.TrimStart('\\')); if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } // New Image File will be uploaded using (var filesStream = new FileStream(Path.Combine(uploads, objTable.Table.Id + extension_new), 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_new; } //End Image TableFromDb.MaxTables = objTable.Table.MaxTables; TableFromDb.Active = objTable.Table.Active; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(objTable)); }