public ActionResult DeleteProject(int projectId) { tb_ProjectInfo aProject = dbContext.tb_ProjectInfos.FirstOrDefault(x => x.ProjectInfoId == projectId); if (aProject != null) { dbContext.tb_ProjectInfos.Remove(aProject); dbContext.SaveChanges(); } return(RedirectToAction("ProjectList")); }
public ActionResult EditProject(tb_ProjectInfo project, HttpPostedFileBase ProjectFile) { tb_ProjectInfo aproject = dbContext.tb_ProjectInfos.FirstOrDefault(x => x.ProjectInfoId == project.ProjectInfoId); try { if (aproject != null) { aproject.ProjectInfoId = project.ProjectInfoId; } string file = ""; if (ProjectFile != null && ProjectFile.ContentLength > 0) { // string fileExtension = Path.GetExtension(ProjectFile.FileName).ToLower(); //var fileName = Guid.NewGuid().ToString("N"); //imagefile.FileName; file = ProjectFile.FileName; string fileLocation = Server.MapPath("~/Project File/") + file; if (System.IO.File.Exists(fileLocation)) { System.IO.File.Delete(fileLocation); } ProjectFile.SaveAs(fileLocation); } var unique = dbContext.tb_ProjectInfos.FirstOrDefault(x => x.CodeName == project.CodeName && x.ProjectInfoId != project.ProjectInfoId); if (unique != null) { ModelState.AddModelError("", "Code Name Already exsist."); return(RedirectToAction("EditProject", new { codeName = true })); } else { // to remove same key already exists in the ObjectStateManager ((IObjectContextAdapter)dbContext).ObjectContext.Detach(aproject); project.UploadFile = file; dbContext.Entry(project).State = EntityState.Modified; dbContext.SaveChanges(); } ViewBag.Massage = "Saved"; } catch (Exception) { return(RedirectToAction("ProjectList")); } ViewBag.Massage = "Try Again"; return(RedirectToAction("ProjectList")); }
public ActionResult SaveProject(tb_ProjectInfo project, HttpPostedFileBase ProjectFile) { try { //Upload File string file = ""; if (ProjectFile != null && ProjectFile.ContentLength > 0) { //string fileExtension = Path.GetExtension(ProjectFile.FileName).ToLower(); // var fileName = Guid.NewGuid().ToString("N"); //imagefile.FileName; file = ProjectFile.FileName; string fileLocation = Server.MapPath("~/Project File/") + file; if (System.IO.File.Exists(fileLocation)) { System.IO.File.Delete(fileLocation); } ProjectFile.SaveAs(fileLocation); } var unique = dbContext.tb_ProjectInfos.FirstOrDefault(x => x.CodeName == project.CodeName); if (unique != null) { return(RedirectToAction("SaveProject", new { codeName = true })); } project.UploadFile = file; dbContext.tb_ProjectInfos.Add(project); dbContext.SaveChanges(); //ViewBag.Massage = "Saved"; } catch (Exception) { //ViewBag.Massage = "Try Again"; return(RedirectToAction("SaveProject")); } return(RedirectToAction("SaveProject")); }