में एक तालिका को सॉर्ट करना मैं सोच रहा था कि लोग एएसपीनेट एमवीसी में एक टेबल को सॉर्ट करने के बारे में कैसे जा रहे थे? मैंने जावास्क्रिप्ट समाधानों के बारे में सुना है जो गैर-पेजेड टेबलों के साथ बहुत अच्छी तरह से काम करते हैं, जैसे कि jquery's table sorter, लेकिन मुझे एक ऐसे समाधान की आवश्यकता है जो पेजेड टेबल के साथ काम करेगी।एएसपीनेट एमवीसी
मैं जिस परियोजना पर काम कर रहा हूं वह वर्तमान में निम्न समाधान का उपयोग करता है, लेकिन मुझे यह बहुत गन्दा लगता है।
नियंत्रक
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>
इस घ के लिए एक ही रास्ता है इस तरह कुछ उबाऊ? यदि कोई व्यक्ति एक अच्छे तरीके से जानता है जिसमें किसी भी पृष्ठ को एक बार में लोड किए जाने वाले सभी रिकॉर्ड शामिल नहीं हैं तो कृपया उदाहरणों से लिंक करें।
क्ष @AlteredConcept बहुत व्यापक है, कि में यू कह नहीं कर रहे हैं क्या यू के साथ सबसे अधिक चिंतित हैं। क्या यह दृश्य (शायद ही, इसकी सुंदर सरलता से), लिनक के साथ एक सामान्य तरीके से सॉर्ट करने का तरीका, पैरामीटर को पकड़ने के लिए सत्र का उपयोग, उस पैरामीटर जैसे "पैरामीटर" का उपयोग। – eglasius
इसके बारे में क्षमा करें। नियंत्रक वास्तव में मुझे सबसे ज्यादा परेशान करता है। मैं प्रत्येक इकाई सूची दृश्य के लिए प्रत्येक नियंत्रक में उस कोड को दोहराना नहीं चाहता हूं। यह कोड का बहुत अधिक नकल है। मैं एक तरीका समझने की कोशिश कर रहा था कि मैं इसे मूल नियंत्रक में रख सकता हूं, लेकिन एक खाली चल रहा था। तो मैं यह देखने के लिए देख रहा था कि – AlteredConcept
सॉर्ट करने के लिए दूसरों के पास एक अच्छा और अधिक सुरुचिपूर्ण दृष्टिकोण था। मेरे सर में दर्द है। –