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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:18:20+00:00 2026-05-16T21:18:20+00:00

I’m using jqGrid to display some data on a page. Within the controller action,

  • 0

I’m using jqGrid to display some data on a page. Within the controller action, we’re using an anonymous object to represent the data that the jqGrid needs. My question is, is there a way we can create a strongly typed object to represent the jqGrid data that we are sending with Json()?

Main reason for this is so that we can do unit testing with the objects that are being sent to it.

Thanks!

EDIT:

[AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GridData(FormCollection form, string alias, string location, string state)
        {
            int pageSize = Convert.ToInt32(form["rows"]);
            int pageIndex = Convert.ToInt32(form["page"]) - 1;
            var deviceList = this._device.GetList(CreateFilter(location,alias,state),this._securityCache.GetSecurityContext(),pageSize,pageIndex);
            int totalResults = deviceList.TotalRecords;
            int totalPages = (int)Math.Ceiling((float)totalResults / (float)pageSize);
            var jsonData = new {
                total = totalPages,
                page = pageIndex + 1,
                records = totalResults,
                rows = (from device in deviceList.Data
                        select new {i = device.Alias,cell = new string[]{device.Alias,device.Location,device.RatePlan,device.State,device.DateCreated.ToString()}}).ToArray()
                }; 
return Json(jsonData);

This above here works, but we can’t unit test the data that is being passed into the Json() method.

var newJsonData = new JsonJQGridReturnData(); 
                newJsonData.total = totalPages;
                newJsonData.page = pageIndex + 1;
                newJsonData.records = totalResults;
                List<JsonJQGridRow> list = new List<JsonJQGridRow>();
                foreach (var device in deviceList.Data)
                {
                    list.Add(new JsonJQGridRow(device.Alias, new string[] { device.Alias, device.Location, device.RatePlan, device.State, device.DateCreated.ToString() }));
                }
                newJsonData.rows = list.ToArray();      
                _cookieHelper.SaveCookie("DeviceListIndex", this._securityCache.GetSecurityContext().UserID.ToString(), COOKIE_PAGE_SIZE_KEY, pageSize.ToString());
                return Json(newJsonData);
            }

Here is my poor attempt at trying to wrap these into strongly typed objects. Unfortunately, running this gives me a “u is undefined” in the jqGrid file. I suspect that this is because the json being passed in is not correctly formatted. Here are the classes….

[DataContract]
    public class JsonJQGridReturnData
    {
        [DataMember]        
        public int total { get; set; }

        [DataMember]
        public int page { get; set; }

        [DataMember]
        public int records { get; set; }

        [DataMember]
        public JsonJQGridRow[] rows { get; set; }
    }

    [DataContract]
    public class JsonJQGridRow
    {
        public JsonJQGridRow(string i, string[] columns)
        {
            this.i = i;
            this.cells = columns;
        }

        [DataMember]
        public string i { get; set; }

        [DataMember]
        public string[] cells { get; set; }
    }
  • 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-16T21:18:20+00:00Added an answer on May 16, 2026 at 9:18 pm

    If I understand your question you can use Generics to do this:

    Model:

    // represents one row in the JQGrid
            class Customer
            {
                public string firstname { get; set; }
                public string lastname { get; set; }
            }
    

    JQGrid class:

        class JQGridData<TModel>
                {
    // add in whatever other properties you want for JQGrid
                    public int responseTime {get; set; };
                    public List<TModel> rows = new List<TModel>(); 
                }
    

    Controller Action :

    public JsonResult GridData(int page)
            {
                var gridData = new JQGridData<Customer>();
    
    // Populate your data here, this is just an example: 
                gridData.rows.Add(new Customer()
                {
                    firstname = "fred", lastname = "pharkas"
                });
    
    // return the result
                return Json(gridData, JsonRequestBehavior.AllowGet); 
            }
    

    Result:

    {    
         responseTime: 0       
          rows: [         
                {
                    firstname: "fred"
                    lastname: "pharkas"
                }
          ]    
    }
    

    Is that what you were asking?

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the

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.