title: jqgrid + EF + MVC: Is it possible to export in excel, using always the same controller action?
body: <p>I am using <strong>jqgrid (standard) with EF 4 + MVC3</strong>. 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 <strong>optimize / re-use</strong> code, if possible.</p>
  
  <p>To generate excel, <strong>I'd like</strong> to use <a href="http://stephenwalther.com/blog/archive/2008/06/16/asp-net-mvc-tip-2-create-a-custom-action-result-that-returns-microsoft-excel-documents.aspx" rel="nofollow noreferrer">this library</a> 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.</p>
  
  <p>About the jqgrid code, I found <a href="https://stackoverflow.com/a/8228383/1129461">this interesting answer</a> by Oleg, but I do not understand if could be applied to my needs.</p>
  
  <p>Unfortunately, by now I only found parts of solutions for excel export with EF MVC, but no solution or complete examples...</p>
  
  <p><strong>Here's the _Index partial view containing my jqgrid</strong></p>
  
  <pre><code>  &lt;table id="mygrid"&gt;&lt;/table&gt;
    &lt;div id="pager2"&gt;&lt;/div&gt;
  
    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`
  </code></pre>
  
  <p><strong>CONTROLLER (BASED ON <a href="https://stackoverflow.com/questions/5500805/asp-net-mvc-2-0-implementation-of-searching-in-jqgrid/5501644#5501644">OLEG'S IMPLEMENTATION</a>)</strong></p>
  
  <pre><code>     public ActionResult jqIndex(string sidx, string sord, int page, int rows, bool _search, string filters, bool excel) // note the excel parameter &lt;&lt;
         {
          var context = new TManagerContext();
          var objectContext = context.ObjectContext();
  
          var set = objectContext.CreateObjectSet&lt;Ticket&gt;();
          var serializer = new JavaScriptSerializer();
  
          Filters f = (!_search || string.IsNullOrEmpty(filters)) ? null : serializer.Deserialize&lt;Filters&gt;(filters);
          ObjectQuery&lt;Ticket&gt; 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);
      }
  </code></pre>
  
  <p><strong>Please sorry if the question could be silly, I am just a (enthusiast) beginner.</strong></p>
  
  <p><strong>Thanks for your precious help!</strong>
  <strong>Best Regards</strong></p>