[英]How to sort the order of the table by the onclick of the heading of the table using web grid in ASP.NET MVC?
I would like to sort the order of the table by the onclick on the heading of the column (Link). 我想按列标题(链接)上的onclick排序表的顺序。 The code is as below,
代码如下
enter image description here 在此处输入图片说明
public ActionResult StockStatus(int? id) { List data = new List(); if (id.HasValue) { data = db.Items.Where(d => d.InStock.Value d.ItemName).ToList(); ViewBag.LesserThan = id; } else { data = db.Items.OrderBy(d => d.ItemName).ToList(); } return View(data); } public ActionResult StockStatus(int id) { var sold = db.Sales.Include(d => d.Items).Include(d => d.Customers).Where(d => d.ItemId == id).OrderByDescending(d => d.InvoiceId).ThenBy(d => d.Items.ItemName).ToList(); return View(sold); }
Can any one of you please help me in this!! 你们任何人都可以在这方面帮助我!
You have next options: 您还有下一个选择:
Hope this helps! 希望这可以帮助!
you can call action method using action link below 您可以使用下面的操作链接调用操作方法
grid.Column(
header: "Qty In stock",
columnName: "qty",
format: (item) => new HtmlString(Html.ActionLink(Html.ActionLink("Sort", "Sortstock", new { Id = item.idAddress,sort="fieldname"})
)
if u get the column name inside the action method you can sort the list 如果您在action方法中获得列名,则可以对列表进行排序
public ActionResult Sortstock(string sort= "", string sortdir="")
{
List<Item> stocks = Student.getStock();
IQueryable<Item> stud = SortIQueryable<Item>(Student.getStock().AsQueryable(), sort, sortdir);
return View(stud);
}
public static IQueryable<T> SortIQueryable<T>(IQueryable<T> data, string fieldName, string sortOrder)
{
if (string.IsNullOrWhiteSpace(fieldName)) return data;
if (string.IsNullOrWhiteSpace(sortOrder)) return data;
var param = Expression.Parameter(typeof(T), "i");
Expression conversion = Expression.Convert(Expression.Property(param, fieldName), typeof(object));
var mySortExpression = Expression.Lambda<Func<T, object>>(conversion, param);
return (sortOrder == "desc") ? data.OrderByDescending(mySortExpression) : data.OrderBy(mySortExpression);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.