2017-07-26 18 views
2

为什么我的名字和日期排序不起作用。当我点击列链接时,我的数据没有任何反应,也没有错误。当我运行调试并逐步完成排序时,它会选择正确的switch语句,然后生成相同的数据。ASP.NET字母排序不起作用。无错误信息

我不确定它为什么给我相同的数据。我使用了所有匹配的表名。

控制器:

// 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()); 
    } 

查看:

@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> 
+5

您的操作返回'certificationContext'(未排序),但排序是在没有使用任何地方的'resources'对象上完成的。 – DavidG

回答

1

你需要知道的第一件事,有2个包含相同数据的上下文信息源的LINQ查询,但使用不同的语法:

第一查询

// 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)); 
0123下面

第二个查询

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

switch语句执行顺利,但似乎从第二查询,而不是第一个搜索结果进行排序:

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()); 

您需要选择2个可用选项之一:

1)更改switch语句排序的第一个查询,而无需改变返回模式查看页:

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)更改模型的返回类型将已排序的第二个查询传递到查看页面:

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

上面的那两个选项具有不同的查询结果,具体取决于您要使用的选项。

+0

非常感谢。我刚才看到了这个。但是,当我昨天正在审查代码时,它碰到了我,我有两个相同的选择语句哈哈。 –