2011-02-02 58 views
0

我正在关注Phil Haack's example on using jQuery Grid with ASP.NET MVC。我有它的工作,它运作良好......除了一个小问题。当我通过ID之外的其他东西对列进行排序时,从服务器返回的JSON数据非常...错误。这是我的控制器方法。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); 
} 

当我排序是PersonID在jsGrid表,我得到这个数据返回(I只是使用的电流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] ...等

这总是在发生并始终保持一致。我想要弄清楚发生了什么事情会有点疯狂,因为它看起来应该是简单的。

回答

0

我发现这里的解决方案:linq to entities orderby strange issue

问题最终的事实,LINQ到实体有麻烦处理字符串茎。当我使用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); 
0

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

+0

谢谢,但这似乎并没有解决问题。 (我可能做错了什么,但我无法让它工作。)有趣的是,我的数据错误是一致的。例如:([1,1],[2,2],[3,3],[4,4])。名字/数字互相翻转。 – JasCav 2011-02-02 22:00:22

+0

@JasCav:你可以包含具有`PersonID`属性的类的定义吗?此外,我不明白你的代码为什么你使用`var scenarios = repository.FindAllPeople()...`,然后使用**人**中的人(`人`而不是`场景`)?还有一点您应该将`i = person.PersonID`替换为`id = person.PersonID`。目前'我'将被忽略,因为`ID`将被用于像1,2,3 ... – Oleg 2011-02-02 22:11:31

1

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