How Do I Sort a Razor Page DYNAMICALLY?

PostAlmostAnything 1 Reputation point
2022-05-02T19:57:26.273+00:00

I am trying to add sorting to a Razor Page by simply passing sort parameters to a class which should ideally use them properly. The problem is that the directions from Microsoft for how to implement sorting in a Razor page do not tell how to do this. The instructions at https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-6.0 come up short by telling people to hard code sorting parameters and does not provide a dynamic solution.

I want to be able to pass any sort parameter from a Razor page to a service class that will then take that parameter and plug it into the ORDER BY part of the query.

My view code is quite simple. It is like this:

string sortBy = "post.Postid";

string sortDirection = "descending";

Posts = await PostService.GetPaginatedResultSorted(CurrentPage, PageSize, sortBy, sortDirection);

My class is far more complicated, so here is a redacted version. As you can see it plugs the sortBy parameter in where I used to simply type by hand post.Postid. However, passing "post.Postid" as a string from the view does not work. I didn't expect it would, but I thought it would crash the page and produce an error message due to that string not being the same type. I don't know what type it really is, but in my experience Microsoft loves to uses types as an excuse to stop processing the query. In this case it simply returns the results unsorted.

public async Task<List<Posts>> GetPaginatedResultSorted(int currentPage, int pageSize, string sortBy, string sortDirection)

{

return await (from post in _context.Posts

where REDACTED

orderby sortBy descending

select post into pts

select new Posts

{

REDACTED }).Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();

}

I tried changing the orderby part to say "sortBy sortDirection" but that results in a runtime error. I would appreciate it if a Microsoft employee would be nice enough to respond by explaining why their example does not say how to dynamically sort anything and how to do so.

Finally, I could not a find a category for this post titled "ASP.Net" or anything like that even though this is allegedly where the ASP.Net forum was moved according to Microsoft.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
{count} votes

2 answers

Sort by: Most helpful
  1. PostAlmostAnything 1 Reputation point
    2022-05-02T20:32:29.213+00:00

    Also, it seems EF is incapable of sorting by average rating even when passing the parameter is not an issue. If the end of the query says ".OrderBy(p => p.Ratingavg).Take(pageSize).ToListAsync();" Then it results in an error too long to paste which I have never seen.

    0 comments No comments

  2. PostAlmostAnything 1 Reputation point
    2022-05-02T20:45:30.45+00:00

    On top of this, it seems EF is not able to both calculate an average rating for each post and sort by that rating without first getting all post data which is not feasible due to the number of posts involved. How can someone calculate the average of all ratings and use them to rank posts WITHOUT having to start the process by getting all posts?

    This is a problem because if I were to use the rating from the paginated results then only the 10 latest posts have their ratings compared.

    The only think I can think of is writing a new task that starts with the ratings table instead of the posts table before figuring out the average rating using the post id in the ratings table and then using navigation if I need specific post data.

    0 comments No comments