I want to let user download data I am displaying elsewhere in the website as reports.
I am using Asp.net core 2.1 rc1 MVC app.
My cunning plan was to create a special view which would render data as a tab delimited text and use response headers to make browser download it instead of displaying HTML. This almost works perfectly.
My "HttpGet" code in the controller looks like this:
Response.Headers.Add("Content-disposition", "attachment; filename=export.tsv");
return View(MyModel);
My razor view looks like this:
@Model IEnumerable<MyModel>
@{
Layout = null;
String LineBreak = Environment.NewLine;
String Tab = "\t";
}
Header1 Header2 ...
@if (Model.Count > 0)
{
foreach (MyModel myModel in Model)
{
@myModel.Field1@Html.Raw(@Tab)@myModel.Field2 ... @Html.Raw(@LineBreak)
}
}
This works splendidly except that this ugly error message appears as a first line in the file:
System.Collections.Generic.List`1[MyApp.Models.MyModel] IEnumerable<MyModel>
The rest of the file is exactly what I need.
Does anyone know how to remove this message? Or if there is something wrong with my general approach...
Full project available here: https://github.com/under3415/ExportError/ (just click on download link and examine the file)