sortdir 不适用于 MVC 3 WebGird 中的枚举数据类型
我有以下结构:
模型
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当前接受的答案并不是您问题的答案。
当您在绑定操作中未提及列名时,枚举似乎不会排序。我通过在将模型绑定到网络网格时提供所有必需的列名称来修复此问题。在此示例中,UserType 属性是一个枚举。
因此,我认为如果您实现以下代码,排序将在不更改控制器的情况下进行:
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.
So I reckon that the sorting will work without changing your controller if you implement to following code:
尝试使用最后排序的列设置 Grid.SortColumn。
控制器代码
查看代码。
Try setting the
Grid.SortColumn
with the last sorted column.Controller code
View code.
我曾多次遇到过这种情况;特别是在将枚举类型绑定到列时。
我发现您可以通过将 ColumnName 值更改为未使用的非枚举类型属性的名称,然后将其设置为正确的值来解决此问题(尽管这是一个严重的混乱)在使用它之前你的控制器:
例如 - 这失败了:
然后将其更改为类似以下内容:
在我的控制器方法中,我然后执行以下操作:
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:
Then change it to something like:
In my controller method I then do: