public ModelRepository() { Project = new Project(); ProjectsList = new List<Project>(); ProjectCategory = new ProjectCategory(); ProjectCategoryList = new List<ProjectCategory>(); Personel = new Personel(); PersonelList = new List<Personel>(); Position = new Position(); PositionList = new List<Position>(); Task = new Task(); TaskList = new List<Task>(); WorkDay = new WorkDay(); WorkDayList = new List<WorkDay>(); }
public ActionResult Create(ModelRepository repository) { Task task = repository.Task; task.ProjectID = repository.Project.ProjectID; task.PersonelID = Int32.Parse(Request.Form.GetValues("PersonelID").FirstOrDefault().ToString()); task.Status = "Standby"; if (ModelState.IsValid) { db.Tasks.Add(task); var day = Request.Form.GetValues("date[]").ToList(); foreach (var item in day) { if(item != string.Empty) { int check = 0; var workday = new WorkDay(); var datetime = DateTime.Parse(item); if (day.Where(it => it == item).Count() > 1) check++; if (db.WorkDays.Any(it => it.Date == datetime && it.PersonelID == task.PersonelID)) { check++; } if (check == 0) { workday.TaskID = task.TaskID; workday.Date = DateTime.Parse(item); workday.PersonelID = task.PersonelID; db.Tasks.Find(task.TaskID).WorkDays.Add(workday); db.SaveChanges(); } else { ViewBag.alert = "เวลาซ้ำกัน"; return RedirectToAction("Create", new { id = repository.Project.ProjectID }); } } } return RedirectToAction("Index", new {id = repository.Project.ProjectID }); } ViewBag.PersonelID = new SelectList(db.Projects.Find(repository.Project.ProjectID).Personels.ToList(), "PersonnelID", "Name", db.Projects.Find(repository.Project.ProjectID).Personels.First().PersonnelID); ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "Name", task.ProjectID); return View(task); }