public ActionResult Create(string area = "", string locale = "", string category = "", string subcategory = "") { var locations = LocationOps.GetActiveLocationsList(); var postTypes = PostTypesOps.GetActivePostTypesList(); var categories = new List <string> { "Please Select a Category" }; categories.AddRange(postTypes .GroupBy(p => p.Category) .Select(l => l.Key) .ToList()); var areas = new List <string> { "Please Select an Area" }; areas.AddRange(locations .GroupBy(l => l.Area) .Select(l => l.Key) .ToList()); var subcategories = new List <string> { "Please Select a Category" }; var locales = new List <string> { "Please Select a Locale" }; if (string.IsNullOrEmpty(category) && !string.IsNullOrEmpty(subcategory)) { category = PostTypesOps.GetCategoryBySubcategoryName(subcategory); } if (string.IsNullOrEmpty(area) && !string.IsNullOrEmpty(locale)) { area = LocationOps.GetAreaByLocale(locale); } if (!string.IsNullOrEmpty(category)) { subcategories.AddRange(PostTypesOps.GetSubCategoriesByCategory(category) .Select(s => s.SubCategory) .OrderBy(l => l) .ToList()); } if (!string.IsNullOrEmpty(area)) { locales.AddRange(LocationOps.GetLocalesByArea(area) .Select(l => l.Locale) .OrderBy(l => l) .ToList()); } return(View(new PostViewModel { Areas = new SelectList(areas, string.IsNullOrEmpty(area) ? "Please Select an Area" : area), Locales = new SelectList(locales, string.IsNullOrEmpty(locale) ? "Please Select a Locale" : locale), Categories = new SelectList(categories, string.IsNullOrEmpty(category) ? "Please Select a Category" : category), SubCategories = new SelectList(subcategories, string.IsNullOrEmpty(subcategory) ? "Please Select a Subcategory" : subcategory) })); }
public ActionResult Index(string area = "", string category = "", string locale = "", string subcategory = "", string query = "", string pageAction = "") { if (!int.TryParse(pageAction, out var pageNo)) { pageNo = 1; } var selectedArea = string.IsNullOrEmpty(area) ? "Please Select an Area" : area; var selectedLocale = string.IsNullOrEmpty(locale) ? "Please Select a Locale" : locale; var selectedCategory = string.IsNullOrEmpty(category) ? "Please Select a Category" : category; var selectedSubcategory = string.IsNullOrEmpty(subcategory) ? "Please Select a Subcategory" : subcategory; var actualArea = area.Equals("Please Select an Area") ? "" : area; var actualLocale = locale.Equals("Please Select a Locale") ? "" : locale; var actualCategory = category.Equals("Please Select a Category") ? "" : category; var actualSubcategory = subcategory.Equals("Please Select a Subcategory") ? "" : subcategory; var posts = PostFilter.FilterPost(actualArea, actualLocale, actualCategory, actualSubcategory, query); var filteredPosts = posts .Select(p => new PostViewModel { Id = p.Id, Title = p.Title, Body = p.Body, Area = p.Location.Area, Locale = p.Location.Locale, Category = p.PostType.Category, Subcategory = p.PostType.SubCategory, CreateDate = p.CreateDate.ToString("MMM dd") }).ToList(); var pageSize = 10; ViewBag.PageSize = pageSize; ViewBag.PageCount = Convert.ToInt32(Math.Ceiling(filteredPosts.Count * 1.0 / pageSize)); if (pageNo < 1) { pageNo = 1; } if (pageNo > ViewBag.PageCount) { pageNo = ViewBag.PageCount; } ViewBag.CurrentPage = pageNo; var pagedPosts = filteredPosts.Skip((pageNo - 1) * pageSize).Take(pageSize); var subcategories = new List <string> { selectedSubcategory }; var locales = new List <string> { selectedLocale }; if (string.IsNullOrEmpty(actualCategory) && !string.IsNullOrEmpty(actualSubcategory)) { actualCategory = PostTypesOps.GetCategoryBySubcategoryName(actualSubcategory); selectedCategory = actualCategory; } if (string.IsNullOrEmpty(actualArea) && !string.IsNullOrEmpty(actualLocale)) { actualArea = LocationOps.GetAreaByLocale(actualLocale); selectedArea = actualArea; } if (!string.IsNullOrEmpty(actualCategory)) { subcategories.AddRange(PostTypesOps.GetSubCategoriesByCategory(actualCategory) .Select(s => s.SubCategory) .OrderBy(l => l) .ToList()); } if (!string.IsNullOrEmpty(actualArea)) { locales.AddRange(LocationOps.GetLocalesByArea(actualArea) .Select(l => l.Locale) .OrderBy(l => l) .ToList()); } return(View(new PostFilterViewModel { Query = query, Posts = pagedPosts.ToList(), Categories = new SelectList(Categories, selectedCategory), Areas = new SelectList(Areas, selectedArea), SubCategories = new SelectList(subcategories, selectedSubcategory), Locales = new SelectList(locales, selectedLocale) })); }