コード例 #1
0
        public async Task <IActionResult> SearchCompanies([FromBody] JObject reqData)
        {
            try
            {
                //get ajax reqData properties
                var    servCat    = JsonConvert.DeserializeObject <int[]>(reqData["servCatIn"].ToString());
                var    serv       = JsonConvert.DeserializeObject <int[]>(reqData["servIn"].ToString());
                int    page       = reqData["subseqPageIdxIn"].Value <int>();
                string sortOption = reqData["sortOptionIn"].Value <string>();

                //get results from DB based search input
                var results2 = await _companyRepo.GetSearchResults(reqData["searchIn"].ToString(), servCat, serv);

                //re-order results based on sort option
                if (sortOption == "rating")
                {
                    results2 = results2.OrderByDescending(b => b.OverallRating);
                }
                else if (sortOption == "nameAZ")
                {
                    results2 = results2.OrderBy(b => b.CompanyName);
                }
                else if (sortOption == "nameZA")
                {
                    results2 = results2.OrderByDescending(b => b.CompanyName);
                }

                //create paginated list. 3rd argument is page size
                var results = await PaginatedList <Company> .CreateAsync(results2, page == 0? 1 : page, 5);

                //if results is not null, return partial view with the results.
                if (results != null)
                {
                    return(PartialView("_SearchResultsPartial", results));
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(null);
        }