public JsonResult Save(CreateProjectViewModel createProjectViewModel) { CreateProject createProject = ViewModels.Helpers.CreateProjectFromCreateProjectViewModel(createProjectViewModel); db.CreateProjects.Attach(createProject); foreach (CreateProjectItemViewModel createProjectItemViewModel in createProjectViewModel.CreateProjectItems) { CreateProjectItem createProjectItem = db.CreateProjectItems.Find(createProjectItemViewModel.ProjectItemId); if (createProjectItem != null) { createProjectItem.ObjectState = ObjectState.Deleted; } } foreach (int projectItemId in createProjectViewModel.CreateProjectItemsToDelete) { CreateProjectItem createProjectItem = db.CreateProjectItems.Find(projectItemId); if (createProjectItem != null) { createProjectItem.ObjectState = ObjectState.Deleted; } } db.SetNewStateObject(); db.SaveChanges(); if (createProject.ObjectState == ObjectState.Deleted) { return(Json(new { newLocation = "/CreateProjects/Index/" })); } string userFlag = ViewModels.Helpers.GetUserFlag(createProjectViewModel.ObjectState, createProject.FirstName); createProjectViewModel = ViewModels.Helpers.CreateProjectViewModelFromCreateProject(createProject); createProjectViewModel.UserFlag = userFlag; return(Json(new { createProjectViewModel })); }
public static CreateProject CreateProjectFromCreateProjectViewModel(CreateProjectViewModel createProjectViewModel) { CreateProject createProject = new CreateProject(); createProject.ProjectId = createProjectViewModel.ProjectId; createProject.FirstName = createProjectViewModel.FirstName; createProject.LastName = createProjectViewModel.LastName; createProject.ObjectState = createProjectViewModel.ObjectState; int tmpProjectItemId = -1; foreach (CreateProjectItemViewModel createProjectItemViewModel in createProjectViewModel.CreateProjectItems) { CreateProjectItem createProjectItem = new CreateProjectItem(); createProjectItem.StartDate = createProjectItemViewModel.StartDate; createProjectItem.EndDate = createProjectItemViewModel.EndDate; createProjectItem.Credits = createProjectItemViewModel.Credits; createProjectItem.ObjectState = createProjectItemViewModel.ObjectState; if (createProjectItemViewModel.ObjectState != ObjectState.Added) { createProjectItem.ProjectItemId = createProjectItemViewModel.ProjectItemId; } else { createProjectItem.ProjectItemId = tmpProjectItemId; tmpProjectItemId--; } createProjectItem.ProjectId = createProjectViewModel.ProjectId; createProject.CreateProjectItems.Add(createProjectItem); } return(createProject); }