Why I am unable to use Filters & Sort at same time? If I try to Filter than by sorting it will remove my FIlter.
Below is my razor page - backend - here I am handdling sort & Filter. Issue is that CurrentSort
& CurrentFilter
lose there value when you hit on button
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" />
</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>
CodePudding user response:
this fixed it for me. I hope they come up with gridview like in old webForms which are simple to use and can handle complex/subs grids.
@Html.DisplayNameFor(model => model.CourseTakenList[0].First_Name)