.net 同时排序和筛选

55ooxyrt  于 2023-01-06  发布在  .NET
关注(0)|答案(1)|浏览(121)

为什么我不能同时使用过滤器和排序?如果我尝试过滤比排序它将删除我的过滤器。
下面是我的剃刀页面-后端-在这里我处理排序和过滤。问题是,CurrentSortCurrentFilter失去了那里的价值,当你点击按钮

public class My_TrainingModel : PageModel
   {
     public IList<CourseTaken_Model> CourseTakenList { get; set; } = default!;

     BindProperty(SupportsGet = true)]
     public string? SearchString { get; set; }
     ...
     public string First_Name_Sort { get; set; }
     public string Last_Name_Sort { get; set; }
     public string Course_Date_Sort { get; set; }
     public string CurrentFilter { get; set; }
     public string CurrentSort { get; set; }
     ...

    public async Task OnGetAsync(string sortOrder, string searchString)
    {   
 IQueryable<CourseTaken_Model> CourseTakenList_Temp = from s in _context.CourseTaken_DbSet
                  where !string.IsNullOrEmpty(s.First_Name)
                  select s;

   CurrentSort = sortOrder;
   CurrentFilter = searchString;

   if (!string.IsNullOrEmpty(searchString))
    {
                CourseTakenList_Temp = CourseTakenList_Temp.Where(s => s.First_Name.ToUpper().Contains(searchString.ToUpper())
                               || s.Last_Name.ToUpper().Contains(searchString.ToUpper()));
   }

     ...
     First_Name_Sort = String.IsNullOrEmpty(sortOrder) ? "First_Name_desc" : ""; // if no sort than sort by first_name 
     First_Name_Sort = sortOrder == "First_Name_asc" ? "First_Name_desc" : "First_Name_asc";
     Last_Name_Sort = sortOrder == "Last_Name_asc" ? "Last_Name_desc" : "Last_Name_asc";
     Course_Date_Sort = sortOrder == "Course_Date_asc" ? "Course_Date_desc" : "Course_Date_asc";

     switch (sortOrder)
     {
                case "First_Name_asc":
                    CourseTakenList_Temp = CourseTakenList_Temp.OrderBy(s => s.First_Name.Replace(" ", ""));
                    break;
                case "First_Name_desc":
                    CourseTakenList_Temp = CourseTakenList_Temp.OrderByDescending(s => s.First_Name.Replace(" ", ""));
                    break;
                case "Last_Name_asc":
                    CourseTakenList_Temp = CourseTakenList_Temp.OrderBy(s => s.Last_Name.Replace(" ", ""));
                    break;
                case "Last_Name_desc":
                    CourseTakenList_Temp = CourseTakenList_Temp.OrderByDescending(s => s.Last_Name.Replace(" ", ""));
                    break;
                case "Course_Date_Asc":
                    CourseTakenList_Temp = CourseTakenList_Temp.OrderBy(s => s.Course_Date);
                    break;
                case "Course_Date_desc":
                    CourseTakenList_Temp = CourseTakenList_Temp.OrderByDescending(s => s.Course_Date);
                    break;
                default:
                    CourseTakenList_Temp = CourseTakenList_Temp.OrderBy(s => s.Last_Name.Replace(" ", ""));
                    break;
        }

            CourseTakenList = await CourseTakenList_Temp.AsNoTracking().Take(100).ToListAsync();
razor page - front end. here I am creating search bar & displaying data in a table: 

    <form asp-page="./My_Training" method="get">
        Find by First or Last name: <input type="text" name="SearchString" value="@Model.CurrentFilter" />
        <input type="submit" value="Filter" class="btn btn-default"  />
    </form>

    <Table>

...
<a asp-page="./My_Training" asp-route-sortOrder="@Model.First_Name_Sort">
                    @Html.DisplayNameFor(model => model.CourseTakenList[0].First_Name)
                </a>
...
 <a asp-page="./My_Training" asp-route-sortOrder="@Model.Last_Name_Sort">
                    @Html.DisplayNameFor(model => model.CourseTakenList[0].Last_Name)
                </a>
'''
<a asp-page="./My_Training" asp-route-sortOrder="@Model.Course_Date_Sort">
                    @Html.DisplayNameFor(model => model.CourseTakenList[0].Course_Date)
                </a>
'''
    </Table>
xuo3flqw

xuo3flqw1#

这为我修复了它。我希望他们能像旧的webForms一样提出gridview,使用简单,可以处理复杂的/subs网格。
@Html.DisplayNameFor(型号=〉型号.已修课程列表[0].名字)

相关问题