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 7069165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:24:45+00:00 2026-05-28T05:24:45+00:00

I have this sample MVC project using jQuery grid. There is only one problem

  • 0

I have this sample MVC project using jQuery grid. There is only one problem I’m encountering, and that is the sort function of jQuery grid.

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MyAjaxSample.Models
{
    public class Candy
    {
        [Key]        
        public long ID { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public string Color { get; set; }
        public bool Expired { get; set; }
    }
}  

Controller: Please see the comment code, where error occurs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyAjaxSample.Models;

namespace MyAjaxSample.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult DynamicGridData(string sidx, string sord, int page, int rows)
        {
            List<Candy> candyList = new List<Candy>();
            for (int i = 0; i <= 50; i++)
            {
                Candy candy = new Candy();
                candy.ID = i;
                candy.Name = "Candy " + i.ToString();
                candy.Price = i * 0.19;
                candy.Color = "Black";
                candy.Expired = false;

                candyList.Add(candy);
            }

            var context = candyList;
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = context.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            // This is not working
            //var candies = context.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize); ;
            var candies = context;

            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,
                rows = (
                    from item in candies
                    select new
                    {
                        i = candies,
                        cell = new string[] { item.ID.ToString(), item.Name, item.Price.ToString(), item.Color, item.Expired.ToString() }
                    }).ToArray()
            };
            return Json(jsonData);
        }
    }
}

View:

<link href="/Content/jquery-ui-1.8.7.css" rel="stylesheet" type="text/css" />
<link href="/Content/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<link href="/Content/ui.multiselect.css" rel="stylesheet" type="text/css" />

<script src="/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>

<script src="/Scripts/grid.locale-en.js" type="text/javascript"></script>
<script src="/Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery("#list").jqGrid({
            url: '/Home/DynamicGridData/',
            datatype: 'json',
            mtype: 'POST',
            colNames: ['ID', 'Name', 'Price', 'Color', 'Expired'],
            colModel: [
        { name: 'ID', index: 'ID', width: 40, align: 'left' },
        { name: 'Name', index: 'Name', width: 40, align: 'left' },
        { name: 'Price', index: 'Price', width: 400, align: 'left' },
        { name: 'Color', index: 'Color', width: 400, align: 'left' },
        { name: 'Expired', index: 'Expired', width: 400, align: 'left' }
        ],
            pager: jQuery('#pager'),
            rowNum: 80,
            rowList: [20, 10, 20, 50],
            sortname: 'ID',
            sortorder: "desc",
            viewrecords: true,
            imgpath: '',
            caption: 'My first grid'
        });
    }); 
</script>  

<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>  

The only problem here is the sorting function. Actually I’ll be using this to Entity Framework, the reason I’ve used List is because (for I think) they have same sortOrder parameters. Anyway I hope someone could help.

  • 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-28T05:24:45+00:00Added an answer on May 28, 2026 at 5:24 am

    Use Demo link to see sample Example.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple mvc project consisting of one table named Persons and that
I am working on a MVC project that is supposed to have one page
I have created a sample project using ASP.NET MVC 3 Web Application (Razor) template.
Assume I have a class that looks like this: class Sample { public string
Say you have an ASP.NET MVC project and are using a service layer, such
I have a relatively simple ASP.net MVC 2 app that is using SubSonic. Everything
In a project using NHibernate, I have this class : public class AdminVAT :
I'm using a masterpage in my ASP.NET MVC project. This masterpage expects some ViewData
I have an ASP.NET MVC project and I have a single action that accepts
Im working on a project in MVC and have enjoyed learning about it. There

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.