简体   繁体   English

将模型发送到控制器动作以导出为CSV

[英]Sending model to controller action to export as csv

I have a large set of data that my users can filter by various options. 我有大量数据,我的用户可以通过各种选项进行过滤。 I'd like to export this set of data as a CSV, but not sure how to send the item to my controller action. 我想将这组数据导出为CSV,但不确定如何将项目发送到控制器操作。

The following code in my index.cshtml file: 我的index.cshtml文件中的以下代码:

@model Project.ViewModels.PeopleIndexViewModel
<a href="@Url.Action("Export", "Person", new { List = Model.People.ToList()})" title="Export to CSV">
    <img src="/content/images/excel.gif" />
</a>

Turns out to be empty when I hit the controller: 当我按下控制器时,结果是空的:

public ActionResult Export(List<Person> List)
{
    // List is empty here...
}

What am I doing wrong? 我究竟做错了什么?

In my opinion it won't work that way. 在我看来,这种方式是行不通的。 I would suggest keeping the List in Session or TempData like that: 我建议像这样将List保留在Session或TempData中:

Session["PeopleList"] = list;

or 要么

TempData["PeopleList"] = list;

This way you might access it between requests. 这样,您可以在两次请求之间访问它。 Remember - storage in Session is for Session duration. 请记住-会话中的存储时间为会话持续时间。 Storage in TempData is only till the next request unless you use Keep method. 除非您使用Keep方法,否则在TempData中的存储只会保留到下一个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM