Pass a datatable model to controller action in MVC
- by user2906420
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