JSON 结果以与预期不同的顺序返回

发布于 2024-10-15 17:04:27 字数 2317 浏览 5 评论 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] [甲、乙、丙] [C、B、A] ...等等

这总是发生并且总是一致的。我有点疯狂地试图弄清楚发生了什么,因为它看起来应该很简单。

I am following Phil Haack's example on using jQuery Grid with ASP.NET MVC. I have it working and it works well...except for one minor problem. When I sort the columns by something other than the ID, the JSON data returned from the server is very...well...wrong. Here's is my Controller method.

[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);
}

When I sort by PersonID in the jsGrid table, I get this data back (I just used the name of the current ID as the name - e.g. 1, One; 2, Two, etc.)

{"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"]}]}

When I sort by PersonName, however, every other row has the order (the ID vs. the name) flipped around. So when I show it in the table, the PersonName is in the ID column and the ID is in the person column. Here is the JSON result.

{"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"]}]}

Anybody have any insight into what I've done wrong that causes this to happen?

Update

So, I have learned that, what is happening, is that my array values are flipping for every other item in the array. For example...if I populate my database with:

[A, B, C]

then for every even-numbered result (or odd, if you're counting from 0), my data is coming back:

[C, B, A]

So, ultimately, my JSON row data is something like:

[A, B, C]
[C, B, A]
[A, B, C]
[C, B, A]
...etc

This is always happening and always consistent. I am going a bit crazy trying to figure out what's going on because it seems like it should be something simple.

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

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

发布评论

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

评论(3

临风闻羌笛 2024-10-22 17:04:27

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

I have the same problem with my data which are INT type.
If elements in my queue (A,B,C) are NVARCHAR type I do not have this problem.
So problem is obviously in SqlFunction.StringConvert function.

情话墙 2024-10-22 17:04:27

尝试使用描述的方法 这里。如果您在 repository.FindAllPeople() 中使用字段而不是属性,您应该查看代码中使用 FieldInfoGetField 的注释部分> 而不是 PropertyInfoGetProperty

Try to use the method described here. If you use fields instead of properties in the repository.FindAllPeople() you should look at the commented part of the code where are used FieldInfo and GetField instead of PropertyInfo and GetProperty.

画骨成沙 2024-10-22 17:04:27

我在这里找到了解决方案: linq toEntity 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);

I found the solution here: linq to entities orderby strange issue

The issue ultimately stems from the fact that Linq to Entities has trouble handling strings. When I was using the SqlFunctions.StringConvert method, this was incorrectly performing the conversion (although, I must admit that I don't fully understand why the order was then switched around).

In either case, per the above post, the solution for fixing the problem was to do the selection locally so that I could "force" Linq to Entities to work with strings properly. From this, my final code is:

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