title: Export to excel not working when using Ajax in MVC
body: <p>Hi i have created export class that converts Gridview to excel file.</p>
  
  <p>See below code:</p>
  
  <p>DownloadFileActionResult Class:</p>
  
  <pre><code>public class DownloadFileActionResult : ActionResult
      {
  
  
          public GridView ExcelGridView { get; set; }
          public string fileName { get; set; }
  
  
          public DownloadFileActionResult(GridView gv, string pFileName)
          {
  
              ExcelGridView = gv;
  
              fileName = pFileName;
  
          }
  
  
  
          public override void ExecuteResult(ControllerContext context)
          {
  
              HttpContext curContext = HttpContext.Current;
              curContext.Response.ClearContent();
              curContext.Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls");
              curContext.Response.ContentType = "application/vnd.ms-excel";
              StringWriter sw = new StringWriter();
              HtmlTextWriter htw = new HtmlTextWriter(sw);
              ExcelGridView.RenderControl(htw);
              curContext.Response.Write(sw.ToString());
              curContext.Response.End();
  
          }
  
      }
  </code></pre>
  
  <p>Jquery-ajax:</p>
  
  <pre><code>function Export(){
  
  
      var search = {};                                        
      search.Name = "MaterialShape";
      search.Description = "";
      search.Address ="";
  
      var url_ = generateURL("/Home/Download");                    //Call Save Controller  and pass details entities  
  
      $.ajax({
          type: "POST",
          url: url_,
          data: search,                                            //details will act as the Entities Model
          traditional: true,
          success: function(data) {
  
  
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
                  alert("error: " + XMLHttpRequest.responseText);
          },
          dataType: 'json'
      });
  
  };
  </code></pre>
  
  <p>Search Parmeter Properties:</p>
  
  <pre><code>public class SearchParams
      {
          public string Name{ get; set; }
          public string Description {get;set;}
          public string Address{get;set;}
          ...
       }
  </code></pre>
  
  <p>And then i implement it on my controller:</p>
  
  <pre><code>     //Export to excel
      public ActionResult Download(SearchParam param)
      {
  
         List&lt;Lookup&gt; lookupList = data.GetLookup(param);
        var grid = new System.Web.UI.WebControls.GridView();
  
         grid.DataSource = lookupList;
         grid.DataBind();
  
         return new DownloadFileActionResult(grid, "test");
  
      }
  </code></pre>
  
  <p>It is working(without search param values) when i type the controller url manually</p>
  
  <pre><code>http://localhost:54928/Home/Download  
  </code></pre>
  
  <p>or using html.action link </p>
  
  <pre><code>&lt;%= Html.ActionLink("Home", "/Download", "Home")%&gt; 
  </code></pre>
  
  <p>but it is not working when i use ajax call</p>
  
  <pre><code>&lt;img src="&lt;%=Url.Content("~/Images/export.png")%&gt;" id="Img1" onclick="Export();" alt="Export" /&gt; 
  </code></pre>
  
  <p>that i really need to use.</p>
  
  <p>I am missing something here..any ideas?</p>
  
  <p>Thanks in Regards</p>