public ActionResult Add(Activities activities) { try { var activity = new Activities() { ParentActivityID = activities.ParentActivityID, ActivityDescription = activities.ActivityDescription, StartDateTime = activities.StartDateTime, EndDateTime = activities.EndDateTime }; using (var activitiesDb = new ActivitiesDbCtxt()) { var parentActivity = activitiesDb.Activities.Find(activity.ParentActivityID); if (parentActivity == null && activities.ActivityID != 0) { return(RedirectToAction("Index")); } activitiesDb.Activities.Add(activity); activitiesDb.SaveChanges(); } return(RedirectToAction("Index")); } catch (Exception ex) { ModelState.AddModelError("ParentActivityId", ex.Message); return(View("Add", activities)); } }
private List <Activities> GetSortedList(int startSortingAt, List <Activities> activitiesList, SortType sortType) { var baseList = activitiesList; if (startSortingAt == 0) { if (sortType == SortType.Ascending) { baseList = baseList.OrderBy(x => x.ActivityDescription).ToList(); } if (sortType == SortType.Descending) { baseList = baseList.OrderByDescending(x => x.ActivityDescription).ToList(); } foreach (var child in baseList) { SortList(sortType, child); } } else { var lookingFor = baseList.Select(item => Activities.Find(item, startSortingAt)).FirstOrDefault(activity => activity != null); SortList(sortType, lookingFor); } return(baseList); }
private SelectList GetCategories(int currentId, out int parentActivityId) { var dbCtxt = new ActivitiesDbCtxt(); var activitiesList = CreateStructure(0, dbCtxt.Activities.ToList()); var lookingFor = activitiesList.Select(item => Activities.Find(item, currentId)).FirstOrDefault(activity => activity != null); var parent = activitiesList.Select(item => Activities.Find(item, lookingFor.ParentActivityID)).FirstOrDefault(activity => activity != null); if (parent != null) { parent.ChildActivities.Remove(lookingFor); } else { activitiesList.Remove(lookingFor); } var childList = GetAllChilds(activitiesList); var categories = childList.Select(x => new SelectListItem { Value = x.ActivityID.ToString(), Text = x.DisplayAsCategory }); var list = categories.ToList(); SelectListItem selected; if (parent != null) { selected = (from category in list where category.Value == parent.ActivityID.ToString() select category).First(); } else { selected = null; } if (selected != null) { selected.Selected = true; list.Insert(0, new SelectListItem { Value = "0", Text = "No parent category" }); parentActivityId = int.Parse(selected.Value); } else { list.Insert(0, new SelectListItem { Value = "0", Text = "No parent category", Selected = true }); parentActivityId = 0; } return(new SelectList(list, "Value", "Text")); }
private void SortList(SortType sortType, Activities firstActivity) { if (firstActivity.ChildActivities.Count == 0) { return; } if (sortType == SortType.Ascending) { firstActivity.ChildActivities = firstActivity.ChildActivities.OrderBy(x => x.ActivityDescription).ToList(); } else { firstActivity.ChildActivities = firstActivity.ChildActivities.OrderByDescending(x => x.ActivityDescription).ToList(); } foreach (var activity in firstActivity.ChildActivities) { SortList(sortType, activity); } }