public async Task <IActionResult> Create([Bind("Id,Name,BuiltBy,Description,Github_Url,Live_Url,ImageFile")] MyProjects_Ar myProjects_Ar) { if (ModelState.IsValid) { //save image to wwwroot/Images/MyProjectsAr string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(myProjects_Ar.ImageFile.FileName); string extension = Path.GetExtension(myProjects_Ar.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("yymmssff") + extension; myProjects_Ar.ImageName = fileName; string path = Path.Combine(wwwRootPath + "/Images/MyProjectsAr/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await myProjects_Ar.ImageFile.CopyToAsync(fileStream); } //insert record _context.Add(myProjects_Ar); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(myProjects_Ar)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } MyProjects_Ar myProjects_Ar2 = await _context.MyProjects_Ar.Where(x => x.Id == id).FirstOrDefaultAsync(); if (myProjects_Ar2 == null) { return(NotFound()); } return(View(myProjects_Ar2)); }
public async Task <IActionResult> Edit(int?id, MyProjects_Ar myProjects_Ar, IFormFile file) { if (id == null) { return(NotFound()); } MyProjects_Ar myProjects_Ar2 = await _context.MyProjects_Ar.Where(x => x.Id == id).FirstOrDefaultAsync(); if (myProjects_Ar2 == null) { return(NotFound()); } if (file != null || file.Length != 0) { string filename = System.Guid.NewGuid().ToString() + ".jpg"; var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot", "Images/MyProjectsAr", filename); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } myProjects_Ar2.ImageName = filename; } myProjects_Ar2.Name = myProjects_Ar.Name; myProjects_Ar2.Description = myProjects_Ar.Description; myProjects_Ar2.BuiltBy = myProjects_Ar.BuiltBy; myProjects_Ar2.Github_Url = myProjects_Ar.Github_Url; myProjects_Ar2.Live_Url = myProjects_Ar.Live_Url; await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }