Dennis Kabugua
Dennis Kabugua

Reputation: 65

sorting and filtering in asp.net core razor pages

I'm following Working with Data in ASP.NET Core tutorial in Microsoft documentation,(Sort, filter, page, and group section) but I have a hard time trying to figure out how the following code works...(I'm new to razor pages and asp.net core).

this is the PageModel for students index page.

namespace Contoso.Pages.Students
{
    public class IndexModel : PageModel
    {
       //

        public string NameSort { get; set; }
        public string DateSort { get; set; }
        public string CurrentFilter { get; set; }
        public string CurrentSort { get; set; }


        public async Task OnGetAsync(string sortOrder,string currentFilter, string searchString, int? pageIndex)
        {
            CurrentSort = sortOrder;
            NameSort = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            DateSort = sortOrder == "Date" ? "date_desc" : "Date";
            if (searchString != null)
            {
                pageIndex = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            CurrentFilter = searchString;

            IQueryable<Student> studentIQ = from s in _context.Students
                                            select s;
            if (!String.IsNullOrEmpty(searchString))
            {
                studentIQ = studentIQ.Where(s => s.LastName.Contains(searchString)
                                       || s.FirstMidName.Contains(searchString));
            }
            switch (sortOrder)
            {
                case "name_desc":
                    studentIQ = studentIQ.OrderByDescending(s => s.LastName);
                    break;
                case "Date":
                    studentIQ = studentIQ.OrderBy(s => s.EnrollmentDate);
                    break;
                case "date_desc":
                    studentIQ = studentIQ.OrderByDescending(s => s.EnrollmentDate);
                    break;
                default:
                    studentIQ = studentIQ.OrderBy(s => s.LastName);
                    break;
            }

        }
    }
}

and this is a from in the index page for the model.

@*other markup commented out*@
<table class="table">
    <thead>
        <tr>
            <th>
                <a asp-page="./Index" asp-route-sortOrder="@Model.NameSort"
                   asp-route-currentFilter="@Model.CurrentFilter">
                    @Html.DisplayNameFor(model => model.Student[0].LastName)
                </a>
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Student[0].FirstMidName)
            </th>
            <th>
                <a asp-page="./Index" asp-route-sortOrder="@Model.DateSort"
                   asp-route-currentFilter="@Model.CurrentFilter">
                    @Html.DisplayNameFor(model => model.Student[0].EnrollmentDate)
                </a>
            </th>
            <th></th>
        </tr>
    </thead>
</table>

@*other markup commented out*@

now my question is how is for example asp-route-sortOrder="@Model.NameSort" related to sortOrder parameter in the OnGetAsync method ? and how exactly does the value sortOrder get set by clicking on the link in the index page?

Upvotes: 2

Views: 6615

Answers (1)

Mike Brind
Mike Brind

Reputation: 30035

how is asp-route-sortOrder="@Model.NameSort" related to sortOrder parameter in the OnGetAsync method ?

The route attribute on the select tag helper allows you to specify values for route data parameters. If the key (sortOrder in this case) is included as part of a route template, the value will appear as a segment in the generated URL. Otherwise it is appended to the URL as a query string value. Those are matched to handler methods by Model Binding. You can read more about how model binding works in Razor Pages here: https://www.learnrazorpages.com/razor-pages/model-binding

Upvotes: 3

Related Questions