public ActionResult Edit(UserFileVM model) { if (model.UserFile == null) { model.UserFile = new UserFile(); } model.UploadFile = UserFiles.GetUploadFile(Request.Files[0]); if (!FluentValidate(model)) { return(RedirectBack()); } UserFile userFile = null; var courseTCs = model.CourseTCs; var groupIds = model.GroupIds; if (model.IsNew) { userFile = model.UserFile; userFile.UserID = User.UserID; userFile.SysFileName = model.UploadFile.Name; var courses = GetCourses(model.CourseTCs); var groups = GetGroups(model.GroupIds); userFile.CourseFiles.AddRange(courses); userFile.GroupFiles.AddRange(groups); FileService.InsertAndSubmit(userFile); } else { var userFileId = model.UserFile.UserFileID; userFile = FileService.GetByPK(userFileId); if (userFile.UserID != User.UserID) { throw new NotOwnerException("File"); } userFile.Update(model.UserFile, x => x.Name, x => x.Description); if (!model.UploadFile.IsEmpty) { userFile.SysFileName = model.UploadFile.Name; } FileService.SubmitChanges(); ChangeCourses(userFileId, courseTCs); ChangeGroups(userFileId, groupIds); } if (!model.UploadFile.IsEmpty) { UserFiles.DeleteFile(userFile.UserFileID); var hpf = Request.Files[0]; hpf.SaveAs(UserFiles.GetUserFileSys(userFile.UserFileID, model.UploadFile.Name)); } return(RedirectToAction(() => List(1))); }