我是 mvc 的新手。我有一个有两种方法的控制器。我想通过单击按钮将列表返回到文本文件。我一直在尝试找出如何将列表传递给 CreateReportFile 方法的方法,但没有运气。它适用于 stringbuilder 对象。
您知道如何在文本文件中显示列表吗?
@Html.ActionLink("Download File", "CreateReportFile");
public FileStreamResult CreateReportFile()
{
//todo: add some data from your database into that string:
var string_with_your_data = string.Empty;
var byteArray = Encoding.ASCII.GetBytes(string_with_your_data);
var stream = new MemoryStream(byteArray);
return File(stream, "text/plain", "Report" + DateTime.Now + ".txt");
}
public List<Student> GetStudents()
{
return new List<Student>()
{
new Student() {firstname="james",lastname="john",emailAddress="james.john@yahoo.com"},
new Student() {firstname="patric",lastname="swayze",emailAddress="patric.swayze@yahoo.com"},
new Student() {firstname="james",lastname="john",emailAddress="james.john@yahoo.com"},
new Student() {firstname="toy",lastname="gas",emailAddress="toy.gas@yahoo.com"}
};
}