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

时间:2011-02-02 16:05:09

标签: json asp.net-mvc-2 linq-to-entities jqgrid dynamic-linq

我关注Phil Haack's example on using jQuery Grid with 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,One; 2,Two等)。

{"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]   ...等

这种情况总是在发生并始终如一。我有点疯狂试图弄清楚发生了什么,因为它似乎应该是简单的事情。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

尝试使用here描述的方法。如果您在repository.FindAllPeople()中使用字段而不是属性,那么您应该查看使用FieldInfoGetField代替PropertyInfo和{{1}的代码的注释部分}。

答案 2 :(得分:0)

我在这里找到了解决方案:linq to entities orderby strange issue

问题最终源于Linq to Entities在处理字符串时遇到问题。当我使用SqlFunctions.StringConvert方法时,这是错误地执行转换(虽然,我必须承认我不完全理解为什么然后切换订单)。

在任何一种情况下,根据上述帖子,解决问题的方法是在本地进行选择,这样我就可以“强制”Linq到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);
相关问题