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