The Intern
The Intern

Reputation: 73

ASP.NET Alphabetic Sort is not working. No Error Message

Why is my name and date sort not working. When I click the column link nothing happens to my data and no error. When I run a debug and step through the sort it selects the right switch statement and then produces the same data.

I am not sure as to why it is giving me the same data. I used all of the matching table names.

Controller:

// GET: Managerial

    public async Task<IActionResult> Index(string sortOrder)
    {

        //QUERY

        ViewBag.FullName = UserInformation.Globals.FullName;

        var ParentORG = _context.CORP_MatrixPositionOLDWay
            .Where(p => p.ParentLAN == UserInformation.Globals.LANID)
            .Select(p => p.ParentOrgLevel.TrimEnd('!'))
            .First();


        var LANlist = _context.CORP_MatrixPositionOLDWay
           .Where(x => x.ChildOrgLevel.StartsWith(ParentORG.ToString()))
           .Select(x => x.ChildLAN)
           .Where(lan =>lan != UserInformation.Globals.LANID);

        var certificationContext = _context.INT_CertificationsXREF
            .Include(i => i.INT_CertificationCategories)
            .Include(i => i.INT_Certifications)
            .Include(i => i.INT_CertificationConferred)
            .Include(i => i.RIM_Resource)
            .Where(i => i.RIM_Resource.LAN == i.RIM_Resource.LAN)
            .Where(i => LANlist.Contains(i.RIM_Resource.LAN));

        //Sorting


            ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
            var resources = from s in _context.INT_CertificationsXREF
                           select s;
        switch (sortOrder)
        {
            case "name_desc":
                resources = resources.OrderByDescending(s => 

s.RIM_Resource.LastName);

                break;
            case "Date":
                resources = resources.OrderBy(s => s.RenewDate);
                break;
            case "date_desc":
                resources = resources.OrderByDescending(s => s.RenewDate);
                break;
            default:
                resources = resources.OrderBy(s => s.RIM_Resource.LastName);
                break;
        }




        return View(await certificationContext.AsNoTracking().ToListAsync());
    }

View:

@model IEnumerable<Certifications.Models.INT_CertificationsXREF>
<link href="~/css/Overide.css" rel="stylesheet" />

@{
    ViewData["Title"] = "Index";
}

<table class="table CDS_Table" Id="myTable">
    <thead>
        <tr>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewData

["NameSortParm"]">@Html.DisplayNameFor(model => model.RIM_Resource)</a>

            </th>

            <th>
                @Html.DisplayNameFor(model => model.INT_Certifications)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.INT_Certifications.Description)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.INT_CertificationCategories.Category)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.INT_CertificationConferred.ConferredBy)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.AwardDate)
            </th>
            <th>
                <a asp-action="Index" asp-route-sortOrder="@ViewData["DateSortParm"]">@Html.DisplayNameFor(model => model.RenewDate)</a>
            </th>

            <th>
                @Html.DisplayNameFor(model => model.Approved)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsActive)
            </th>


            <th></th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.ID"> @Html.DisplayFor(modelItem => item.RIM_Resource.FirstName) @Html.DisplayFor(modelItem => item.RIM_Resource.LastName) </a>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.INT_Certifications.Certification)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.INT_Certifications.Description)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.INT_CertificationCategories.Category)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.INT_CertificationConferred.ConferredBy)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.AwardDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.RenewDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Approved)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.IsActive)
                </td>
                <td>
                    <form asp-action="Index">
                        <div class="form-actions no-color">
                            @if (item.Approved == true)
                            {
                            <input type="submit" value="Revoke" class="btn btn-default" asp-route-id="@item.ID" />
                            }
                            else
                            {
                            <input type="submit" value="Approve" class="btn btn-default" asp-route-id="@item.ID" />
                            }
                        </div>
                    </form>
                </td>
                <td>
                    <a asp-action="Delete" asp-route-id="@item.ID">Deactivate</a>
                </td>


            </tr>
        }
    </tbody>

</table>

Upvotes: 2

Views: 436

Answers (1)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

First thing you need to know, there are 2 LINQ queries with same data context source but using different syntax:

First Query

// SELECT ... FROM ... WHERE ... with JOIN
var certificationContext = _context.INT_CertificationsXREF
        .Include(i => i.INT_CertificationCategories)
        .Include(i => i.INT_Certifications)
        .Include(i => i.INT_CertificationConferred)
        .Include(i => i.RIM_Resource)
        .Where(i => i.RIM_Resource.LAN == i.RIM_Resource.LAN)
        .Where(i => LANlist.Contains(i.RIM_Resource.LAN));

Second Query

// equivalent: SELECT * FROM INT_CertificationsXREF
var resources = from s in _context.INT_CertificationsXREF
                select s;

The switch statement below executes smoothly but seems to sort results from second query instead of first one:

switch (sortOrder)
{
     case "name_desc":
          resources = resources.OrderByDescending(s => s.RIM_Resource.LastName);
          break;
     case "Date":
          resources = resources.OrderBy(s => s.RenewDate);
          break;
     case "date_desc":
          resources = resources.OrderByDescending(s => s.RenewDate);
          break;
     default:
          resources = resources.OrderBy(s => s.RIM_Resource.LastName);
          break;
}

And what you're returned to view page is model from the first query which is still unsorted:

return View(await certificationContext.AsNoTracking().ToListAsync());

You need to choose one of 2 available options:

1) Change switch statement to sort first query, without changing returned model to view page:

switch (sortOrder)
{
     case "name_desc":
          certificationContext = certificationContext.OrderByDescending(s => s.RIM_Resource.LastName);
          break;
     case "Date":
          certificationContext = certificationContext.OrderBy(s => s.RenewDate);
          break;
     case "date_desc":
          certificationContext = certificationContext.OrderByDescending(s => s.RenewDate);
          break;
     default:
          certificationContext = certificationContext.OrderBy(s => s.RIM_Resource.LastName);
          break;
}

2) Change model return type to pass sorted second query into view page:

return View(await resources.AsNoTracking().ToListAsync());

Those 2 options above have different query results depending which option you want to use.

Upvotes: 1

Related Questions