public async Task <IActionResult> Create([Bind("Id,Name,Smarts,ImageFile,URL")] SustainableChemistryWeb.ViewModels.FunctionalGroupViewModel functionalGroupView) { if (ModelState.IsValid) { string name = System.IO.Path.GetFileName(functionalGroupView.ImageFile.FileName); FunctionalGroup appFunctionalGroup = new FunctionalGroup() { Name = functionalGroupView.Name, Smarts = functionalGroupView.Smarts, Image = "Images/FunctionalGroups/" + name, URL = functionalGroupView.URL }; if (functionalGroupView.ImageFile.Length > 0) { using (var stream = new System.IO.FileStream(_hostingEnvironment.WebRootPath + "/Images/FunctionalGroups/" + name, System.IO.FileMode.Create)) { await functionalGroupView.ImageFile.CopyToAsync(stream); stream.Close(); } } _context.Add(appFunctionalGroup); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(functionalGroupView)); }
// GET: FunctionalGroups/Edit/5 public async Task <IActionResult> Edit(long?id) { if (id == null) { return(NotFound()); } var appFunctionalgroup = await _context.AppFunctionalgroup.FindAsync(id); if (appFunctionalgroup == null) { return(NotFound()); } SustainableChemistryWeb.ViewModels.FunctionalGroupViewModel functionalGroupView = new SustainableChemistryWeb.ViewModels.FunctionalGroupViewModel() { Name = appFunctionalgroup.Name, Smarts = appFunctionalgroup.Smarts, Image = appFunctionalgroup.Image, URL = appFunctionalgroup.URL }; return(View(functionalGroupView)); }
public async Task <IActionResult> Edit(long id, [Bind("Id,Name,Smarts,Image,ImageFile,URL")] SustainableChemistryWeb.ViewModels.FunctionalGroupViewModel functionalGroupView) { if (ModelState.IsValid) { if (id != functionalGroupView.Id) { return(NotFound()); } } if (id != functionalGroupView.Id) { return(NotFound()); } var functionalGroupToUpdate = await _context.AppFunctionalgroup .SingleOrDefaultAsync(m => m.Id == id); if (await TryUpdateModelAsync <FunctionalGroup>( functionalGroupToUpdate, "", r => r.Name, r => r.Smarts, r => r.URL)) { try { var fileName = _hostingEnvironment.WebRootPath + "/" + functionalGroupToUpdate.Image; if (functionalGroupView.ImageFile != null) { if (System.IO.File.Exists(fileName)) { System.IO.File.Delete(fileName); } functionalGroupToUpdate.Image = "Images/FunctionalGroups/" + Guid.NewGuid().ToString() + System.IO.Path.GetFileName(functionalGroupView.ImageFile.FileName); using (var stream = new System.IO.FileStream(_hostingEnvironment.WebRootPath + "/" + functionalGroupToUpdate.Image, System.IO.FileMode.Create)) { await functionalGroupView.ImageFile.CopyToAsync(stream); stream.Close(); } } //else //{ // functionalGroupToUpdate.Image = "Images/FunctionalGroups/" + Guid.NewGuid().ToString() + ".jpg"; // System.IO.StreamReader image = new System.IO.StreamReader(_hostingEnvironment.WebRootPath + "/Images/Reactions/th.jpg"); // using (var stream = new System.IO.FileStream(_hostingEnvironment.WebRootPath + "/" + functionalGroupToUpdate.Image, System.IO.FileMode.Create)) // { // await image.BaseStream.CopyToAsync(stream); // stream.Close(); // } //} await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AppFunctionalgroupExists(functionalGroupToUpdate.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(functionalGroupView)); }