Sortdir not working on enum data type in MVC 3 WebGird

I have the following structure:

Model

public class EventEntry : LogEntry
{
    public EventType Type { get; set; }

    public string Source { get; set; }
}

public enum EventType : int
{
    Information = 1,
    Warning = 2,
    Error = 3
} 

View

<div id="grid">
@{
    var grid = new WebGrid(canPage: true, rowsPerPage: Ctrl.PageSize, canSort: true, ajaxUpdateContainerId: "grid");
    grid.Bind(Model.Events, rowCount: Model.TotalRecords, autoSortAndPage: false);

    grid.Pager(WebGridPagerModes.All);
    @grid.GetHtml(htmlAttributes: new { id="grid" },
            columns: grid.Columns(
            grid.Column("Type"),
            grid.Column("Source"));    
}
</div>

controller

public ActionResult Index(int? page, string sort, string sortdir) {...}

When I click on the Source column that has a type row, sordir will change from β€œASC” to β€œDESC,” but when I try the same in the Type column, sordir will always return β€œASC”.

+5
source share
3 answers

The current accepted answer is not the answer to your problem.

Enumerations don't seem to be sorted unless you specify a column name in the bind operation. I fixed this by providing all the necessary column names when binding my model to webgrid. The UserType property is an enumeration in this example.

        var webgrid = new WebGrid(rowsPerPage: 25);

        webgrid.Bind(Model, new[] { "FirstName", "MiddleName", "SurName", "UserType" });

        var columns = webgrid.Columns(
            webgrid.Column("FirstName", "Voornaam"),
            webgrid.Column("MiddleName", "Tussenvoegsels"),
            webgrid.Column("SurName", "Achternaam"),
            webgrid.Column("UserType", "Type gebruiker"),                
        );

, , , :

<div id="grid">
@{
    var grid = new WebGrid(canPage: true, rowsPerPage: Ctrl.PageSize, canSort: true, ajaxUpdateContainerId: "grid");
    grid.Bind(Model.Events, new[] { "Type", "Source" }, rowCount: Model.TotalRecords, autoSortAndPage: false);

    grid.Pager(WebGridPagerModes.All);
    @grid.GetHtml(htmlAttributes: new { id="grid" },
            columns: grid.Columns(
            grid.Column("Type"),
            grid.Column("Source"));    
}
</div>
+4

Grid.SortColumn .

ViewData["lastsortedcol"] = Request["sort"];

.

var grid = new WebGrid();

grid.Bind(source: userItems.PagedSet, rowCount: userItemsForSale.TotalCount,autoSortAndPage:false);

grid.SortColumn = (string)ViewData["lastsortedcol"] ;

Response.Write(grid.GetHtml(          
  columns: grid.Columns
      (
            grid.Column(columnName: "ItemName", header: "ItemName", format: (item) => Html.Label(((UserItemForSale)item.Value).ItemDetails.Name)),
            grid.Column(columnName: "Quantity", header: "Quantity", format: (item) => Html.Label(((UserItemForSale)item.Value).Qty + ""))
      )
));
+1

It happened to me several times; especially when binding an enumeration type to a column.

I found that you can work around this problem (although this is a serious problem) by changing the ColumnName value to the name of the unused non-enumeration type property and then setting it to the correct value in your controller before using this:

eg. - this fails:

grid.Column("PaymentMethod", "Loan Delivery Method", item => string.Format("{0}", EnumHelper.GetFirstValueFromMetaDataAttribute(item.PaymentMethod, Constants.GENERALMETADATATAG))),

Then change it to something like:

grid.Column("WaitForDocsNoOfRetries", "Loan Delivery Method", item => string.Format("{0}", EnumHelper.GetFirstValueFromMetaDataAttribute(item.PaymentMethod, Constants.GENERALMETADATATAG))),

In my controller method, I do the following:

sort = sort == "WaitForDocsNoOfRetries" ? "PaymentMethod": sort;
+1
source

All Articles