public void SaveDuAn(Tbproject model) { if (model.ProjectId == 0) { context.Tbproject.Add(model); } else { Tbproject project = context.Tbproject .FirstOrDefault(p => p.ProjectId == model.ProjectId); if (project != null) { project.ProjectName = model.ProjectName; project.CategoryId = model.CategoryId; project.ServiceId = model.ServiceId; project.ProjectHidden = model.ProjectHidden; project.ProjectLink = model.ProjectLink; project.ProjectContent = model.ProjectContent; project.ProjectImage = model.ProjectImage; } } context.SaveChanges(); }
public ActionResult DuAnAdd(Project model) { string uniqueFileName = null; // If the Photo property on the incoming model object is not null, then the user // has selected an image to upload. if (model.Project_UpImage != null) { // The image must be uploaded to the images folder in wwwroot // To get the path of the wwwroot folder we are using the inject // HostingEnvironment service provided by ASP.NET Core string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "uploadimages"); // To make sure the file name is unique we are appending a new // GUID value and and an underscore to the file name uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Project_UpImage.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); // Use CopyTo() method provided by IFormFile interface to // copy the file to wwwroot/images folder model.Project_UpImage.CopyTo(new FileStream(filePath, FileMode.Create)); } Tbproject project = new Tbproject { ProjectName = model.ProjectName, CategoryId = model.CategoryId, ServiceId = model.ServiceId, ProjectHidden = model.ProjectHidden, ProjectImage = model.ProjectImage, ProjectContent = model.ProjectContent, // Store the file name in PhotoPath property of the employee object // which gets saved to the Employees database table ProjectLink = "/uploadimages/" + uniqueFileName }; db.SaveDuAn(project); return(RedirectToAction("DuAn")); }