好的,我想我已经想出了解决方案:
创建自己的 HtmlTableGridRenderer:
public class CustomTableGridRenderer<TViewModel> : HtmlTableGridRenderer<TViewModel> where TViewModel : class
{
protected override void RenderHeaderText(GridColumn<TViewModel> column)
{
if (IsSortingEnabled && column.Sortable)
{
string sortColumnName = GenerateSortColumnName(column);
bool isSortedByThisColumn = GridModel.SortOptions.Column == sortColumnName;
var sortOptions = new GridSortOptions
{
Column = sortColumnName
};
if (isSortedByThisColumn)
{
sortOptions.Direction = (GridModel.SortOptions.Direction == SortDirection.Ascending)
? SortDirection.Descending
: SortDirection.Ascending;
}
else //default sort order
{
sortOptions.Direction = column.InitialDirection ?? GridModel.SortOptions.Direction;
}
var routeValues = CreateRouteValuesForSortOptions(sortOptions, GridModel.SortPrefix);
//Re-add existing querystring
foreach (var key in Context.RequestContext.HttpContext.Request.QueryString.AllKeys.Where(key => key != null))
{
if (!routeValues.ContainsKey(key))
{
routeValues[key] = Context.RequestContext.HttpContext.Request.QueryString[key];
}
}
var link = HtmlHelper.GenerateLink(Context.RequestContext, RouteTable.Routes, column.DisplayName, null, null, null, routeValues, null);
RenderText(link);
}
else
{
base.RenderHeaderText(column);
}
}
}
...只需更换
if(! routeValues.ContainsKey(key))
{
routeValues[key] = Context.RequestContext.HttpContext.Request.QueryString[key];
}
... 和routeValues[key] = Context.RequestContext.HttpContext.Request.QueryString[key];
并像这样使用您的新渲染:
@Html.Grid()...RenderUsing(new CustomTableGridRenderer())