0

我正在关注Phil Haack 关于将 jQuery Grid 与 ASP.NET MVC 结合使用的示例。我让它工作了,而且效果很好……除了一个小问题。当我按 ID 以外的其他内容对列进行排序时,从服务器返回的 JSON 数据非常……嗯……错误。这是我的控制器方法。

[HttpPost]
public ActionResult PeopleData(string sidx, string sord, int page, int rows)
{
    int pageIndex = Convert.ToInt32(page) - 1;
    int pageSize = rows;
    int totalRecords = repository.FindAllPeople().Count();
    int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

    var people = repository.FindAllPeople()
        .OrderBy(sidx + " " + sord)
        .Skip(pageIndex * pageSize)
        .Take(pageSize);

    var jsonData = new
    {
        total = totalPages,
        page = page,
        records = totalRecords,
        rows = (
            from person in people
            select new
            {
                i = person.PersonID,
                cell = new List<string> { SqlFunctions.StringConvert((double) person.PersonID), person.PersonName }
            }
        ).ToArray()
    };

    return Json(jsonData);
}

当我在 jsGrid 表中按 PersonID 排序时,我得到了这个数据(我只是使用当前 ID 的名称作为名称 - 例如 1、一;2、二等)

{"total":1,"page":1,"records":6,"rows":[{"i":1,"cell":[" 1","One"]},{"i":2,"cell":["         2","Two"]},{"i":3,"cell":["         3","Three"]},{"i":4,"cell":["         4","Four"]},{"i":5,"cell":["         5","Five"]},{"i":6,"cell":["         6","Six"]}]}

但是,当我按 PersonName 排序时,每隔一行都会翻转顺序(ID 与名称)。所以当我在表格中显示它时,PersonName 在 ID 列中,ID 在 person 列中。这是 JSON 结果。

{"total":1,"page":1,"records":6,"rows":[{"i":5,"cell":[" 5","Five"]},{"i":4,"cell":["Four","    4"]},{"i":1,"cell":["         1","One"]},{"i":6,"cell":["Six","      6"]},{"i":3,"cell":["         3","Three"]},{"i":2,"cell":["Two","    2"]}]}

任何人都知道我做错了什么导致这种情况发生?

更新

所以,我了解到,正在发生的事情是我的数组值正在为数组中的每个其他项目翻转。例如......如果我用以下内容填充我的数据库:

[A、B、C]

然后对于每个偶数结果(或奇数,如果你从 0 开始计数),我的数据就会返回:

[C、B、A]

因此,最终,我的 JSON 行数据类似于:

[A, B, C] [C, B, A] [A, B, C] [C, B, A] ...等

这种情况一直在发生并且始终一致。我有点疯狂地试图弄清楚发生了什么,因为它看起来应该很简单。

4

3 回答 3

1

我的 INT 类型数据也有同样的问题。如果我的队列(A、B、C)中的元素是 NVARCHAR 类型,我没有这个问题。所以问题显然出在 SqlFunction.StringConvert 函数中。

于 2011-02-09T13:37:51.067 回答
0

我在这里找到了解决方案:linq to entity orderby 奇怪的问题

该问题最终源于 Linq to Entities 无法处理字符串这一事实。当我使用 SqlFunctions.StringConvert 方法时,这是错误地执行转换(尽管我必须承认我不完全理解为什么顺序会被切换)。

在任何一种情况下,根据上述帖子,解决问题的解决方案是在本地进行选择,以便我可以“强制”Linq to Entities 正确处理字符串。由此,我的最终代码是:

var people = repository.FindAllPeople()
             .OrderBy(sidx + " " + sord)
             .Skip(pageIndex * pageSize)
             .Take(pageSize);

// Due to a problem with Linq to Entities working with strings,
// all string work has to be done locally.
var local = people.AsEnumerable();
var rowData = local.Select(person => new
        {
            id = person.PersonID,
            cell = new List<string> { 
                person.PersonID.ToString(),
                person.PersonName
            }
        }
    ).ToArray();

var jsonData = new
{
    total = totalPages,
    page = page,
    records = totalRecords,
    rows = rowData
};

return Json(jsonData);
于 2011-02-09T16:57:01.370 回答
0

尝试使用此处描述的方法。如果您在 中使用字段而不是属性,repository.FindAllPeople()则应查看代码的注释部分 where are used FieldInfoandGetField而不是PropertyInfoand GetProperty

于 2011-02-02T20:16:34.973 回答