title: Export to CSV from MVC Controller and View displays CSV raw data on Page
body: <p>I've just got my MVC view export to Excel working great, however, because i'm setting location.href this is leaving me with a page full of CSV data rather than the neat grid results before the user hit EXPORT button. </p>
  
  <p>I'm trying to think how to change the following script to do what its doing but to leave the page alone. I tried making a call to the search to go back to server again but at runtime user see's the CSV on the webpage momentarily, which is not good.</p>
  
  <p>Any ideas much appreciated, Cheers</p>
  
  <pre><code>    $(function() {
          $('#exportButton').click(function() {
              var url = $('#AbsolutePath').val() + 'Waste.mvc/Export';
  
              var data = {
                  searchText: $('#SearchTextBox').val().toString(),
                  searchTextSite: $('#SearchTextBoxSite').val().toString(),
                  StartDate: $('#StartDate').val(),
                  EndDate: $('#EndDate').val()
              };
              $('#ResultsList').load(url, data, function() {
                  $('#LoadingGif').empty();
                  location.href = url + "?searchText=" + data.searchText + "&amp;searchTextSite=" + data.searchTextSite + "&amp;StartDate=" + data.StartDate + "&amp;EndDate=" + data.EndDate;
              });
              //Search(); this fixes because grid is displayed again after csv results
          });
      });
  </code></pre>
  
  <p>My Controller Code:</p>
  
  <pre><code>    public FileStreamResult Export(string searchText, string searchTextSite, string StartDate, string EndDate)
      {
  
          var searchResults = getSearchResults(searchText, searchTextSite, StartDate, EndDate);
          HttpContext.Response.AddHeader("content-disposition", "attachment; filename=Export.csv");
  
          var sw = new StreamWriter(new MemoryStream());
  
          sw.WriteLine("\"Ref\",\"Source\",\"Collected\"");
          foreach (var line in searchResults.ToList())
          {
              sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\"",
                                         line.WasteId,
                                         line.SourceWasteTypeId.ToDescription(),
                                         line.CollectedDate.ToShortDateString()));
          }
          sw.Flush();
          sw.BaseStream.Seek(0, SeekOrigin.Begin);
  
          return new FileStreamResult(sw.BaseStream, "text/csv");
          // return File(sw.BaseStream, "text/csv", "report.csv"); Renders the same result
  
      }
  </code></pre>