如何在控制器中使用多个排序顺序

时间:2019-03-23 09:55:48

标签: asp.net-mvc-5 entity-framework-6

我在一个控制器中有3个页面的多个排序列表,但是当我在一个表中而不是在索引页面中排序时。我返回到索引页面。

我认为可以更改排序顺序名称以使其不同。

 public async Task<ActionResult> DashBoard(string sortOrder, string currentFilter,string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var clinicalAssets = from s in db.ClinicalAssets.Include(c => c.ModelName) select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                clinicalAssets = clinicalAssets.Where(s => s.SerialNo.Contains(searchString)
                                       || s.PoNo.Contains(searchString));
            }

            switch (sortOrder)
            {
                case "name_desc":
                    clinicalAssets = clinicalAssets.OrderByDescending(s => s.PoNo);
                    break;
                case "Date":
                    clinicalAssets = clinicalAssets.OrderBy(s => s.PurchaseDate);
                    break;
                case "date_desc":
                    clinicalAssets = clinicalAssets.OrderByDescending(s => s.WarrantyEndDate);
                    break;
                default:
                    clinicalAssets = clinicalAssets.OrderBy(s => s.ClinicalAssetID);
                    break;
            }
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(await clinicalAssets.ToPagedListAsync(pageNumber, pageSize));
        }


        public async Task<ActionResult> Arjo(string sortOrder2, string currentFilter2, string searchString2, int? page)
        {
            ViewBag.CurrentSort = sortOrder2;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder2) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder2 == "Date" ? "date_desc" : "Date";

            if (searchString2 != null)
            {
                page = 1;
            }
            else
            { 
            }

            ViewBag.CurrentFilter = searchString2;

            var clinicalAssets = from s in db.ClinicalAssets
                                 where  (s.AssetTypeID.Equals (2) || s.AssetTypeID.Equals(3) )
                                 select s;



            if (!String.IsNullOrEmpty(searchString2))
            {
                clinicalAssets = clinicalAssets.Where(s => s.SerialNo.Contains(searchString2)
                                       || s.PoNo.Contains(searchString2));
            }



            switch (sortOrder2)
            {
                case "name_desc":
                    clinicalAssets = clinicalAssets.OrderByDescending(s => s.PoNo);
                    break;
                case "Date":
                    clinicalAssets = clinicalAssets.OrderBy(s => s.PurchaseDate);
                    break;
                case "date_desc":
                    clinicalAssets = clinicalAssets.OrderByDescending(s => s.WarrantyEndDate);
                    break;
                default:
                    clinicalAssets = clinicalAssets.OrderBy(s => s.ClinicalAssetID);
                    break;
            }
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(await clinicalAssets.ToPagedListAsync(pageNumber, pageSize));
        }

        public async Task<ActionResult> Index(string sortOrder1, string currentFilter1, string searchString1, int? page)
        {
            ViewBag.CurrentSort = sortOrder1;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder1) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder1 == "Date" ? "date_desc" : "Date";

            if (searchString1 != null)
            {
                page = 1;
            }
            else
            {
                searchString1 = currentFilter1;
            }

            ViewBag.CurrentFilter = searchString1;

            var clinicalAssets = from s in db.ClinicalAssets.Include(c => c.ModelName) select s;

            if (!String.IsNullOrEmpty(searchString1))
            {
                clinicalAssets = clinicalAssets.Where(s => s.SerialNo.Contains(searchString1)
                                       || s.PoNo.Contains(searchString1));
            }

            switch (sortOrder1)
            {
                case "name_desc":
                    clinicalAssets = clinicalAssets.OrderByDescending(s => s.PoNo);
                    break;
                case "Date":
                    clinicalAssets = clinicalAssets.OrderBy(s => s.PurchaseDate);
                    break;
                case "date_desc":
                    clinicalAssets = clinicalAssets.OrderByDescending(s => s.WarrantyEndDate);
                    break;
                default:
                    clinicalAssets = clinicalAssets.OrderBy(s => s.ClinicalAssetID);
                    break;
            }
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(await clinicalAssets.ToPagedListAsync(pageNumber, pageSize));
        }

我希望看到每个页面都过滤自己的表而不是其他页面的表。

1 个答案:

答案 0 :(得分:0)

我走在正确的轨道上,我还需要更新@ html.Actionlink和sortParm。

  public async Task<ActionResult> Index(string sortOrder1, string currentFilter1, string searchString1, int? page1)
        {
            ViewBag.CurrentSort1 = sortOrder1;
            ViewBag.NameSortParm1 = String.IsNullOrEmpty(sortOrder1) ? "name_desc" : "";
            ViewBag.DateSortParm1 = sortOrder1 == "Date" ? "date_desc" : "Date";

            if (searchString1 != null)
            {
                page1 = 1;
            }
            else
            {
                searchString1 = currentFilter1;
            }

            ViewBag.CurrentFilter1 = searchString1;

            var clinicalAssets = from s in db.ClinicalAssets.Include(c => c.ModelName) select s;

            if (!String.IsNullOrEmpty(searchString1))
            {
                clinicalAssets = clinicalAssets.Where(s => s.SerialNo.Contains(searchString1)
                                       || s.PoNo.Contains(searchString1));
            }

            switch (sortOrder1)
            {
                case "name_desc":
                    clinicalAssets = clinicalAssets.OrderByDescending(s => s.PoNo);
                    break;
                case "Date":
                    clinicalAssets = clinicalAssets.OrderBy(s => s.PurchaseDate);
                    break;
                case "date_desc":
                    clinicalAssets = clinicalAssets.OrderByDescending(s => s.WarrantyEndDate);
                    break;
                default:
                    clinicalAssets = clinicalAssets.OrderBy(s => s.ClinicalAssetID);
                    break;
            }
            int pageSize = 10;
            int pageNumber = (page1 ?? 1);
            return View(await clinicalAssets.ToPagedListAsync(pageNumber, pageSize));
        }