public ActionResult Edit(string id, Project project, string ManagerId, HttpPostedFileBase upload) { try { if (ModelState.IsValid) { var oldProject = _unitOfWork.ProjectRepositry.SingleOrDefault(p => p.Id == id); oldProject.Name = project.Name; oldProject.StartTime = project.StartTime; oldProject.DeadTime = project.DeadTime; oldProject.Description = project.Description; //update manager in that table var newProjectManager = new ProjectManagerTable() { ProjectID = id, ManagerID = ManagerId }; //Guid is used as a file name on the basis that it can pretty much guarantee uniqueness if (upload != null && upload.ContentLength > 0) { var photo = new MyProjectFile() { FileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(upload.FileName), MyFileType = MyFileType.Photo }; string physicalPath = Server.MapPath("~/images/" + photo.FileName); // save image in folder upload.SaveAs(physicalPath); oldProject.ProjectFiles = new List <MyProjectFile>(); oldProject.ProjectFiles.Add(photo); } _unitOfWork.ProjectMangerRepositry.Add(newProjectManager);//this is for add or update _unitOfWork.Complete(); } if (User.IsInRole("Admin")) { return(RedirectToAction("Index")); } else { return(RedirectToAction("ShowProjectsForManager")); } } catch (Exception exception) { return(View()); } }
public ActionResult Create(CreateProjectViewModels viewmodel, string __UserId__, int status, HttpPostedFileBase upload) { try { if (viewmodel == null) { return(HttpNotFound()); } //if (!ModelState.IsValid) //{ // var errors = ModelState.Values.SelectMany(vr => vr.Errors); // var s = ModelState.Values; // var v = new IndexViewModels() // { // Project = new Project(), // Users = _unitOfWork.UserRepositry.GetAll().ToList(), // Clients = _unitOfWork.ClientRepositry.GetAll().ToList() // }; // return View(v); //} //Add To Projects..we need user,client,to add them to project //get client //Note , here i send Name of client but i name ClientId in that form //var client = _unitOfWork.ClientRepositry.SingleOrDefault(c => c.Name == ClientId); //only corspond data will be set ,so users,clinet will be null var newProject = new Project() { Id = viewmodel.Project.Id, Name = viewmodel.Project.Name, DeadTime = viewmodel.Project.DeadTime, StartTime = viewmodel.Project.StartTime, Status = (StatusEnum)Enum.ToObject(typeof(StatusEnum), status), Description = viewmodel.Project.Description, ClientId = 1, Users = new List <ApplicationUser>(),//this is must ProjectFiles = new List <MyProjectFile>() }; //get user manager if (!__UserId__.IsEmpty()) { var userManager = _unitOfWork.UserRepositry.SingleOrDefault(u => u.Id == __UserId__); //Add this manger //newProject.Users.Add(user); var pm = new ProjectManagerTable() { ManagerID = userManager.Id, ProjectID = viewmodel.Project.Id }; _unitOfWork.ProjectMangerRepositry.Add(pm); } //Guid is used as a file name on the basis that it can pretty much guarantee uniqueness if (upload != null && upload.ContentLength > 0) { var photo = new MyProjectFile() { FileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(upload.FileName), MyFileType = MyFileType.Photo }; string physicalPath = Server.MapPath("~/images/" + photo.FileName); // save image in folder upload.SaveAs(physicalPath); newProject.ProjectFiles.Add(photo); } _unitOfWork.ProjectRepositry.Add(newProject); _unitOfWork.Complete(); return(RedirectToAction("Index")); } catch (Exception ex) { return(Content(ex.Message)); } }