我想知道人们如何在asp.net mvc中对表进行排序? 我听说过JavaScript解决方案可以很好地处理非分页表,比如jquery的表分类器,但是我需要一个可以与分页表一起工作的解决方案。在asp.net中对表进行排序MVC
我正在处理的项目目前使用以下解决方案,但我觉得它非常混乱。
控制器
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, string propertyName)
{
return OrderingHelper(source, propertyName, true, false);
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string propertyName)
{
return OrderingHelper(source, propertyName, false, true);
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string propertyName)
{
return OrderingHelper(source, propertyName, true, 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", "Sort", new { parameter = "Name"})%>
</th>
<th>
<%=Html.ActionLink("Status", "Sort", new { parameter = "Status" })%>
</th>
<th>
<%=Html.ActionLink("Notes", "Sort", 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>
这是d的唯一途径像这样的东西? 如果有人知道一种更好的方式,不需要将所有记录一次加载到页面上,那么请链接到示例。
@AlteredConcept的q是过于宽泛,在u的不是说什么ü最关心的。它是否是视图(几乎没有,因为它非常简单),以linq的通用方式进行排序的方式,使用session来保存参数,使用“参数”或其解析。 – eglasius 2010-01-13 18:01:37
对不起。管制员是最让我困扰的。我不想在每个控制器中为每个实体列表视图重复所有代码。这是很多重复的代码。我试图找出一种方法,我可以把它放在一个基本的控制器,但运行一个空白。所以我期待着看看别人是否有更好的更优雅的分类方法 – AlteredConcept 2010-01-13 19:32:09
谢谢。我头疼。 – 2010-01-17 17:20:59