// GET: People public ActionResult Index(int?id) { //create new viewModel for People/Index var viewModel = new PeopleIndexData(); // viewModel.People must include all People viewModel.People = db.People; //if some persone was select if (id != null) { // take persone what was selected Person selectedPeson = viewModel.People.Where(i => i.ID == id).Single(); ViewBag.ID = id; // if Developer return project for this developer if (selectedPeson.PersonType == PersonType.Developer) { viewModel.Projects = viewModel.People.Where(i => i.ID == id).Single().Projects; } // if Manager get all project where ManagerID = selected Person ID if (selectedPeson.PersonType == PersonType.Manager) { viewModel.Projects = db.Projects.Where(i => i.ManagerID == id); } } return(View(viewModel)); }
// GET: Projects public ActionResult Index(string sortOrder, string searchString, string currentSearch, int?id, int?SelectedManager, int?CurrentFilter) { var viewModel = new PeopleIndexData(); // if (searchString == null) { searchString = currentSearch; } ViewBag.CurrentSearch = searchString; // if (SelectedManager == null) { SelectedManager = CurrentFilter; } ViewBag.CurrentFilter = SelectedManager; // Creating DropDown List Content for filtering by Manager var managers = db.People.Where(i => i.PersonType == PersonType.Manager).OrderBy(q => q.Firstname).ToList(); ViewBag.SelectedManager = new SelectList(managers, "ID", "FullName", SelectedManager); int managerID = SelectedManager.GetValueOrDefault(); //get all project filtered by manager var projects = db.Projects.Where(c => !SelectedManager.HasValue || c.ManagerID == managerID) .Include(p => p.ActionCompany).Include(p => p.CustomerCompany).Include(p => p.Manager); // try find content in Name or Decription fields in all projects allready filtered by manager if (!String.IsNullOrEmpty(searchString)) { projects = projects.Where(s => s.Name.Contains(searchString) || s.Description.Contains(searchString)).Where(c => !SelectedManager.HasValue || c.ManagerID == managerID); } // Get sortOrder ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name" : ""; ViewBag.StartDateSortParm = sortOrder == "StartTime" ? "StartTime_desc" : "StartTime"; ViewBag.EndDateSortParm = sortOrder == "EndTime" ? "EndTime_desc" : "EndTime"; ViewBag.PrioritySortParm = sortOrder == "Priority" ? "Priority_desc" : "Priority"; // Sorting projects switch (sortOrder) { case "Name": projects = projects.OrderByDescending(s => s.Name); break; case "Priority": projects = projects.OrderBy(s => s.Priority); break; case "StartTime": projects = projects.OrderBy(s => s.StartTime); break; case "EndTime": projects = projects.OrderBy(s => s.EndTime); break; case "Priority_desc": projects = projects.OrderByDescending(s => s.Priority); break; case "StartTime_desc": projects = projects.OrderByDescending(s => s.StartTime); break; case "EndTime_desc": projects = projects.OrderByDescending(s => s.EndTime); break; default: projects = projects.OrderBy(s => s.Name); break; } // add filtered and sorted projects to viewModel viewModel.Projects = projects; // if some project was selected if (id != null) { ViewBag.ProjectID = id.Value; try { // get all developer for selected project and add to viewModel // can be null in case when list of projects filtered viewModel.People = viewModel.Projects.Where(i => i.ProjectID == id.Value).Single().Developers; } catch (Exception) { // } } return(View(viewModel)); }