将数据表模型传递给MVC中的控制器操作

时间:2013-11-06 15:40:35

标签: c# jquery asp.net-mvc asp.net-mvc-3

代码:

@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();
}

我想允许用户在单击按钮时导出CSV文件。应该将数据表传递给方法getCSV。请有人帮助我。谢谢

我不介意使用Tempdata存储数据表,然后在控制器中访问它

1 个答案:

答案 0 :(得分:0)

您需要FileStreamResult

类似的东西:

[HttpPost]
public FileStreamResult GetCSV(someType param)
{
   var dt = MyGetDt(param);
   MemoryStream stream = Export.GetCSV(dt);

   var filename = "ExampleCSV.csv";
   var contenttype = "text/csv";
   return new FileStreamResult(stream, contenttype)
   {FileDownloadName = filename};
}

希望这有帮助!