Pass a datatable model to controller action in MVC

Posted by user2906420 on Stack Overflow See other posts from Stack Overflow or by user2906420
Published on 2013-11-06T15:40:35Z Indexed on 2013/11/06 15:53 UTC
Read the original article Hit count: 149

Filed under:
|
|
|

Code:

@model System.Data.DataTable

<button type="submit" class="btn btn-primary"  data-dismiss="modal" aria-hidden="true"> Download</button>



[HttpPost]
public void getCSV(DataTable dt)
{
   MemoryStream stream = Export.GetCSV(dt);

   var filename = "ExampleCSV.csv";
   var contenttype = "text/csv";
   Response.Clear();
   Response.ContentType = contenttype;
   Response.AddHeader("content-disposition", "attachment;filename=" + filename);
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.BinaryWrite(stream.ToArray());
   Response.End();
}

I want to allow the user to export a CSV file when the button is clicked on. the datatable should be passed to the method getCSV. Please can someone help me. thanks

I do not mind using Tempdata to store the datatable and then access it in the controller

© Stack Overflow or respective owner

Related posts about c#

Related posts about jQuery