Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Contoso - Sort a Table on Details Page

$
0
0

Hi,

While following the learning package at https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/crud, I want to know what changes to be made in the following code so as to have the Course Details table be available in the sortable manner (like on the Index page at https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page). I can call the Details method with an argument delivering the sort manner required but how to have the var student use the OrderBy & OrderByDescending since the same are complained by the VS2013 to be not IQueryable in nature

public async Task<IActionResult> Details(int? id)
{
    if (id == null)
    {
        return NotFound();
    }

    var student = await _context.Students
        .Include(s => s.Enrollments)
            .ThenInclude(e => e.Course)
        .AsNoTracking()
        .SingleOrDefaultAsync(m => m.ID == id);

    if (student == null)
    {
        return NotFound();
    }

    return View(student);
}

While code for the Index page, like the following, works great for the var students:

public async Task<IActionResult> Index(string sortOrder)
{
    ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
    var students = from s in _context.Students
                   select s;
    switch (sortOrder)
    {
        case "name_desc":
            students = students.OrderByDescending(s => s.LastName);
            break;
        case "Date":
            students = students.OrderBy(s => s.EnrollmentDate);
            break;
        case "date_desc":
            students = students.OrderByDescending(s => s.EnrollmentDate);
            break;
        default:
            students = students.OrderBy(s => s.LastName);
            break;
    }
    return View(await students.AsNoTracking().ToListAsync());
}

Thanks in advance.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>