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

  • Home
  • SEARCH
  • 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 7529329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:37:58+00:00 2026-05-30T04:37:58+00:00

I am using jqgrid (standard) with EF 4 + MVC3 . I’d like to

  • 0

I am using jqgrid (standard) with EF 4 + MVC3. I’d like to implement excel export and if possible using the same action controller used to populate the grid.
I wonder if is it possible / logical to pass an additional parameter, for example. Which method you would suggest me?
I ask this question because I am still approaching to implement excel export and I’d like to optimize / re-use code, if possible.

To generate excel, I’d like to use this library by Dr Stephen Walther, which has three types of output and allows to define headers too. Please tell me if you find it valid for my purpose.

About the jqgrid code, I found this interesting answer by Oleg, but I do not understand if could be applied to my needs.

Unfortunately, by now I only found parts of solutions for excel export with EF MVC, but no solution or complete examples…

Here’s the _Index partial view containing my jqgrid

  <table id="mygrid"></table>
  <div id="pager2"></div>

  jQuery("#mygrid").jqGrid({
url:'controller/jqIndex',
datatype: "json",
colNames:['id','field1', ...],
colModel:[
    {name:'id',index:'id', width:55},
    {name:'field1',index:'field1', width:90},
            ...
],
rowNum:10,
rowList:[10,20,30],
pager: '#pager2',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption:"modal jquery + jqgrid test"}); 
jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});

//TODO
???
...some code to call the controller action with the `excel` parameter set `true`

CONTROLLER (BASED ON OLEG’S IMPLEMENTATION)

     public ActionResult jqIndex(string sidx, string sord, int page, int rows, bool _search, string filters, bool excel) // note the excel parameter <<
       {
        var context = new TManagerContext();
        var objectContext = context.ObjectContext();

        var set = objectContext.CreateObjectSet<Ticket>();
        var serializer = new JavaScriptSerializer();

        Filters f = (!_search || string.IsNullOrEmpty(filters)) ? null : serializer.Deserialize<Filters>(filters);
        ObjectQuery<Ticket> filteredQuery = (f == null ? (set) : f.FilterObjectSet(set));

        filteredQuery.MergeOption = MergeOption.NoTracking; // we don't want to update the data


        int totalRecords = filteredQuery.Count();

        var pagedQuery = filteredQuery.Skip("it." + sidx + " " + sord, "@skip",
                                    new ObjectParameter("skip", (page - 1) * rows))
                             .Top("@limit", new ObjectParameter("limit", rows));

        int pageIndex = Convert.ToInt32(page) - 1;
        int pageSize = rows;

        int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

        var queryDetails = (from e in pagedQuery
                            select new
                            {
                                e.TicketID,
                                e.field1,
                                ...
                            }).ToList();

        var result = new
        {
            total = totalPages,
            page = page,
            records = totalRecords,
            rows = (from e in queryDetails
                    select new
                    {
                        id = e.TicketID,
                        cell = new string[]
                        {
                            e.field1,
                            ...
                        }

                    }).ToArray()
        };

         if (excel) {
            ExportExcel(result); // if possible, pass filter parameters too, column order, etc...
         }

        return Json(result, JsonRequestBehavior.AllowGet);
    }

Please sorry if the question could be silly, I am just a (enthusiast) beginner.

Thanks for your precious help!
Best Regards

  • 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-30T04:37:59+00:00Added an answer on May 30, 2026 at 4:37 am

    Larry – A few comments.

    1. You shouldn’t be doing that much logic in your controller. Move all
      of that business logic to another class/service. Then your action
      method would be just a few lines. A quick example

    public JsonResult jqIndex(string sidx, string sord, int page, int rows, 
                              bool _search, string filters){
            return JSON(this.GridQueryService.GetJQGrid(sidx,sord,page,rows,_search,filters), JsosnRequestBehavior.AllowGet);
            }
    

    2.I know you don’t want to repeat code (which point 1 helps) but there are many parameters and things here that simply do not apply to Excel (page, rows).

    3.Passing boolean parameters to change how things function can get messy fast. Lets assume that you now need to pass more/less data to the Excel file, now you have nested conditions all over the place and Unit Testing would just be crappy.

    4.An excel action method will should have a FileResult return type, not a
    JSON result (I guess they are all action results, but this makes your intention all the more clear in your code. Your definition should be something like


    public FileResult GetExcelFile(string sidx, string sord, bool _search, 
                                   string filters){
                  //do stuff to return Excel
            }
    

    If you create your Service in point one in such a way that you have two methods that return different items, but share a common query/search base function, then you are really staying Dry while following the Single Responsibility Principle. An example of this service might be (very rough example, should give you some things to think about):

    public class GridQueryService{
       public YourViewModel GetJQGrid(sidx, page, row, _search, filters){
          //Get the base data 
          var myData = this.GetGridData(sidx, _search, filters);
          //Create your view model and return it back to controller
    } 
       public StreamWriter GetExcelFIle(sidx, _search, filters){
          //Get the base data 
          var myData = this.GetGridData(sidx, _search, filters);
          //Create your Excel file and return it to the controller
    }
    
        private ObjectQuery<Ticket> GetGridData(string sidx, bool _search, string filters){
         //do your data grabbing here - you never return the raw data back to anything outside
         //of this service, so it should be ok to make private
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jqgrid (standard) with EF 4 + MVC3 . I'd like to
I'd like to implement my own delete functionality in jqGrid. I'm currently using the
I am using jqGrid control in an ASP.NET application. Export to Excel feature is
I'm using jqGrid to display some data on a page. Within the controller action,
I am using jqgrid on EF4 MVC3 (C#). I based search on this @Oleg
hi i am using jqgrid and want to do something like if i set
Im using jqgrid wherein i need to pass addtional data to the controller while
I'm using jqGrid with mvc 2 like this: jQuery(#extension_grid).jqGrid({ url: '/Extension/Report', datatype: json, direction:
I'm using jqGrid 4.2 with the filterToolbar , which works great. I'd like to
I'm building a simple mvc3 app using jqgrid. My form currently calls a function

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.