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 "Source" column that is of type string, the sordir will change from "ASC" to "DESC" but when I try the same thing on the "Type" column sordir will always return "ASC".

Answers


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

It appears that enums are not sorted when you do not mention the columnname in the bind operation. I fixed this by providing all the required column names when binding my model to the 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"),                
        );

So I reckon that the sorting will work without changing your controller if you implement to following code:

<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>

Try setting the Grid.SortColumn with the last sorted column.

Controller code

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

View code.

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 + ""))
      )
));

I have had this happen a number of times; specifically when binding an enumeration type to a column.

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

E.g. - 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 then do:

sort = sort == "WaitForDocsNoOfRetries" ? "PaymentMethod": sort;

Need Your Help

The process cannot access the file because it is being used by another process.-error while writing to a file in vb.net

vb.net

in my vb.net code im using streamwriter to write to a file that has been given as input to the form.

How to add native library to "java.library.path" with Eclipse launch (instead of overriding it)

java eclipse path nativelibrary

I got a native library that needs to be added to java.library.path. With JVM argument -Djava.library.path=path... I can set the path as I want.