加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在asp.net MVC中排序表

发布时间:2020-12-15 23:53:01 所属栏目:asp.Net 来源:网络整理
导读:我想知道人们在asp.net mvc中如何排序表? 我听说过与非分页表(比如jquery的表格排序器)相当不错的javascript解决方案,但是我需要一个能与分页表一起使用的解决方案. 我正在开展的项目目前使用以下解决方案,但我发现它非常混乱. 调节器 public ActionResult
我想知道人们在asp.net mvc中如何排序表?
我听说过与非分页表(比如jquery的表格排序器)相当不错的javascript解决方案,但是我需要一个能与分页表一起使用的解决方案.

我正在开展的项目目前使用以下解决方案,但我发现它非常混乱.

调节器

public ActionResult Sort(string parameter)
{  

 IEnumerable<IProduct> list;

 if (Session["Model"] != null)
  list = (IEnumerable<IProduct>)Session["Model"]).ToList<IProduct>();
 else
  list = _service.GetAll();

 if (Session["parameter"] == null && Session["sortDirection"] == null)
 {
  //set the parameter and set the sort to desc
  Session["parameter"] = parameter;
  Session["sortDirection"] = "DESC";
 }
 else if (Session["parameter"] != null) //already set so not the first time
 {
  //same parameter sent
  if (Session["parameter"].ToString().Equals(parameter))
  {
   //check sort direction and reverse
   if (Session["sortDirection"].ToString().Equals("DESC"))
    Session["sortDirection"] = "ASC";
   else
    Session["sortDirection"] = "DESC";
  }
  else //different parameter sent
  {
   Session["sortDirection"] = "DESC";
   Session["parameter"] = parameter;
  }
 }

 if (Session["sortDirection"].CompareTo("ASC") == 0)
  list = Models.ContollerHelpers.SortingHelper.OrderBy(list.AsQueryable(),column);
 else
  list = Models.ContollerHelpers.SortingHelper.OrderByDescending(list.AsQueryable(),column);

 return View("Results",list.ToList);
}

帮手

public class Helper()
{
 private static IOrderedQueryable<T> OrderingHelper<T>(IQueryable<T> source,string propertyName,bool descending,bool anotherLevel)
 {
  ParameterExpression param = Expression.Parameter(typeof(T),string.Empty); // I don't care about some naming
  MemberExpression property = Expression.PropertyOrField(param,propertyName);
  LambdaExpression sort = Expression.Lambda(property,param);

  MethodCallExpression call = Expression.Call(
   typeof(Queryable),(!anotherLevel ? "OrderBy" : "ThenBy") + (descending ? "Descending" : string.Empty),new[] { typeof(T),property.Type },source.Expression,Expression.Quote(sort));

  return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(call);
 }

 public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source,string propertyName)
 {
  return OrderingHelper(source,propertyName,false,false);
 }

 public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source,true,false);
 }

 public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source,true);
 }

 public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source,true);
 }
}

列表显示

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Models.Interface.IProduct>>" %>
<% Session["model"] = Model; %>
 <table>
    <tr>
   <th>
    Edit Details
   </th>
   <th>
    <%=Html.ActionLink("Id","Sort",new {parameter ="Id"}) %>
   </th>
   <th>
    <%=Html.ActionLink("Name",new { parameter = "Name"})%>
   </th>
   <th>
    <%=Html.ActionLink("Status",new { parameter = "Status" })%>
   </th>
   <th>
    <%=Html.ActionLink("Notes",new { parameter = "Notes"})%>
   </th>
  </tr>
  <% foreach (var item in Model){ %>

   <tr>
    <td>
     <%= Html.ActionLink("Edit","Edit",new {  id=item.Id }) %> |
    </td>
    <td>
     <%= Html.Encode(item.Id) %>
    </td>
    <td>
     <%= Html.Encode(item.Name) %>
    </td>
    <td>
     <%= Html.Encode(item.Status) %>
    </td>
    <td>
     <%= Html.Encode(item.Notes) %>
    </td> 
   </tr>

  <% } %>   
    </table>

这是做这样的事情的唯一方法吗?
如果有人知道一个更好的方法,不需要将所有记录一次加载到一个页面,那么请链接到示例.

解决方法

查看DataTables @ DataTables这将让您浏览结果并通过简单的设置进行查询.它与ajax和json数据一起工作.看样品.希望这将帮助您.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读