你好, 我正在开发 ASP.NET MVC 3 应用程序。
我有一个观点
var grid = new WebGrid(rowsPerPage: 10, ajaxUpdateContainerId: "GridDiv",canPage: true,canSort: true);
grid.Bind(source: Model);
grid.Pager(WebGridPagerModes.All);
@grid.GetHtml(htmlAttributes: new { id="grid" },
columns: grid.Columns(
grid.Column("Name"),
grid.Column("Age"),
grid.Column("Sex")
)
在 Controller 中,我有一个自定义的排序算法来对数据进行排序。 我有自定义的升序排序和自定义的降序排序。
我希望当用户单击列标题时按照我自定义的排序算法对行进行排序,而不是内置一个。
为此我尝试了以下方法(我采用“sortdir”并相应地处理它)
Controller
public ActionResult Persons(string sortdir)
{
PersonsListModel = GetAllPersonsList();
if(sortdir=="ASC")
return View(MyAscendingCustomSortAlgorithm(PersonsListModel ));
else
return View(MyDescendingCustomSortAlgorithm(PersonsListModel ));
}
MyAscendingCustomSortAlgorithm
和 MyDescendingCustomSortAlgorithm
是返回按我的自定义算法排序的列表的函数。
当页面加载时,列表排序正确,但当我点击标题时,排序就乱七八糟了。我进行了调试,一切正常。
我的问题是我怎样才能让它工作,并且仍然保持正确的分页
我也尝试设置 canSort: false
但是我不能再点击标题了。
非常感谢您的帮助
最佳答案
您是否尝试过在 Bind() 方法中关闭 autoSortAndPage 参数?
grid.Bind(source: Model, autoSortAndPage : false);
我知道您想保留现有的分页,但我不确定是否有办法同时拥有这两者。您可能需要在操作方法中手动执行分页。
关于c# - ASP.NET MVC 3 使用升序、降序选项自定义排序 WebGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10014460/