sortdir 不适用于 MVC 3 WebGird 中的枚举数据类型

发布于 2024-11-01 22:32:29 字数 932 浏览 0 评论 0原文

我有以下结构:

模型

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

    public string Source { get; set; }
}

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

视图

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

控制器

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

当我单击字符串类型的“源”列时,sordir 将从“ASC”更改为“DESC”,但是当我在“类型”列上尝试相同的操作时,sordir 将会始终返回“ASC”。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪也成诗 2024-11-08 22:32:29

当前接受的答案并不是您问题的答案。

当您在绑定操作中未提及列名时,枚举似乎不会排序。我通过在将模型绑定到网络网格时提供所有必需的列名称来修复此问题。在此示例中,UserType 属性是一个枚举。

        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>

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>
久而酒知 2024-11-08 22:32:29

尝试使用最后排序的列设置 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 + ""))
      )
));

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 + ""))
      )
));
尝蛊 2024-11-08 22:32:29

我曾多次遇到过这种情况;特别是在将枚举类型绑定到列时。

我发现您可以通过将 ColumnName 值更改为未使用的非枚举类型属性的名称,然后将其设置为正确的值来解决此问题(尽管这是一个严重的混乱)在使用它之前你的控制器:

例如 - 这失败了:

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

然后将其更改为类似以下内容:

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

在我的控制器方法中,我然后执行以下操作:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文