Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3877190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:28:08+00:00 2026-05-19T22:28:08+00:00

I am following Phil Haack’s example on using jQuery Grid with ASP.NET MVC .

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:28:09+00:00Added an answer on May 19, 2026 at 10:28 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following Phil Haacks tutorial on using JQGrid with ASP.Net MVC. My application
I have recently started using ASP.Net MVC 3 RC 2 and have attempted to
I really like the way ASP.NET MVC works. I'd love to implement it on
Sorry for the length, but a picture is worth 1000 words: In ASP.NET MVC
I have been sharing database variables using the following code: Namespace DataAccessVariables Public Class
I'm developing an asp.net (classic) application trying to implement the MVP pattern using this
I am playing around with VirtualPC and following Phil Haack's instructions for how to
What is the final/best recommendation for how to serve favicon.ico in ASP.NET MVC? I
I'm having a look at the following code http://aperiodic.net/phil/scala/s-99/p26.scala Specifically def flatMapSublists[A,B](ls: List[A])(f: (List[A])
I have the following string: coolStr = '<!-- \\n @author Phil R\\n @date 6.5.2012\\n

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.