1

I make this code for downloading a cvs file with database's elements but I can't pass the database.
If I run the function, browser downloads a cvs with a default row.

Can you help??

Controll

public FileContentResult DownloadCSV(List<Movie> List)
{
            string csv = "\"ID\",\"Title\",\"Release Date\",\"Genere\",\"Price\",\"Rating\" \n";
            foreach (Movie item in List)
            {
                csv = csv + String.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\" \n",
                                           item.Id,
                                           item.Title,
                                           item.ReleaseDate,
                                           item.Genre,
                                           item.Price,
                                           item.Rating);
            }
            //StringWriter sw = new StringWriter();
            //sw.WriteLine("\"ID\",\"Title\",\"Release Date\",\"Genere\",\"Price\",\"Rating\"");
            return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv","Report123.csv");
        }

View page

@model Database.Models.MovieGenreViewModel

@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<form asp-controller="Movies" asp-action="Index" method="get">
    <p>

        <select asp-for="MovieGenre" asp-items="Model.Genres">
            <option value="">All</option>
        </select>

        Title: <input type="text" asp-for="SearchString" />
        <input type="submit" value="Filter" />

        @*<a href="MoviesController/DownloadCSV">Download CSV</a>*@
        <a asp-action="DownloadCSV" asp-route-list ="@Model.Movies">Edit</a>
    </p>
</form>

1 Answer 1

1

Your DownloadCSV action is expecting a List of Movies, which I think is null.

You can change the method as follows:

public FileContentResult DownloadCSV()
{
            string csv = "\"ID\",\"Title\",\"Release Date\",\"Genere\",\"Price\",\"Rating\" \n";
            List<Movie> List = db.Movies; //get this list from database 
            foreach (Movie item in List)
            {
                csv = csv + String.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\" \n",
                                           item.Id,
                                           item.Title,
                                           item.ReleaseDate,
                                           item.Genre,
                                           item.Price,
                                           item.Rating);
            }
            //StringWriter sw = new StringWriter();
            //sw.WriteLine("\"ID\",\"Title\",\"Release Date\",\"Genere\",\"Price\",\"Rating\"");
            return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv","Report123.csv");
        }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, but i don't remember my database's name. Where can i see the name?
I create DB with EF
Thank you very very much i do my job

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.