title: export to excel from ajax request asp.net MVC kendo UI
body: <p>I am working on a asp.net MVC with kendo UI.
  I want a custom button to install the data from a specific date into Excel. 
  I cannot use the ".Excel" in the grid because I need to filter the data before I install them. </p>
  
  <p>I tried to connect to a method with ajax request:</p>
  
  <pre><code> toolbar.Template(
              @&lt;div&gt;
                  &lt;label&gt;@WebResources.LogFilterText&lt;/label&gt;
                  @(Html.Kendo().DatePicker()
                                                  .Name("datepicker")
                                                  .Value(DateTime.Today)
                                                  .Format("dd.MM.yyyy")
                  )
                  @(Html.Kendo().Button()
                                          .Name("excelButton")
                                          .Events(e =&gt; e.Click("getExcel"))
                                          .Content("To Excel")
                  )
              &lt;/div&gt;
              )
  </code></pre>
  
  <p>this is the javscript function:</p>
  
  <pre><code>function getExcel() {
          var url = '/LogAdmin/GetExcel/';
          $.ajax({
              url: url,
              data: { id: kendo.toString($("#datepicker").data("kendoDatePicker").value(), "dd.MM.yyyy") }, //parameters go here in object literal form
              type: 'GET',
              datatype: 'json',
              success: function (data) {
                  alert("da");
                  $("#test").html(data);
              },
              error: function () { alert("Error has been accured."); }
          });
  
      }
  </code></pre>
  
  <p>and here is the controller method:</p>
  
  <pre><code>public ActionResult GetExcel([Bind(Prefix = "id")] string date)
          {
  
              // result is a valid list from Linq. 
              GridView gridview = new GridView();
              gridview.DataSource = result;
              gridview.DataBind();
  
              // Clear all the content from the current response
              Response.ClearContent();
              Response.Buffer = true;
              // set the header
              Response.AddHeader("content-disposition", "attachment; filename = itfunda.xls");
              Response.ContentType = "application/ms-excel";
              Response.Charset = "";
              // create HtmlTextWriter object with StringWriter
              using (StringWriter sw = new StringWriter())
              {
                  using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                  {
                      // render the GridView to the HtmlTextWriter
                      gridview.RenderControl(htw);
                      // Output the GridView content saved into StringWriter
                      Response.Output.Write(sw.ToString());
                      Response.Flush();
                      Response.End();
                  }
              }
              return View();
          }
  </code></pre>
  
  <p>I do not get an error, and I receive an html table. if I show it on a div then it looks a like a correct table. 
  but how to save it to excel? anybody can help me?</p>