2011-02-02 59 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函數中。