0
votes

I am new to web development with Razor pages .net core VS 2017.

I have index page that lists my data in the form of rows & columns using EF core Model.

I am also providing data filtering options via DropDowns and search box.

I want to export this tabular filtered/unfiltered data to excel. --- For which i think i can use NPOI nuget package (http://www.talkingdotnet.com/import-export-excel-asp-net-core-2-razor-pages/).

I want to do this using asp-page-handler with button click.

Questions: 1. Will this be GET request or POST ?

  1. How to pass data (e.g. model object with all data) to the page handler ?

Any help is Appreciated.

Thanks.

2

2 Answers

0
votes

For your first question about using GET or POST, both methods can be used but I personally use the POST method for cases like this.

For your second question I would not send to server all the filtered data but I would pass the filtering information which will be used in order to generate the data and export them to an Excel file.

Update 1

I made a simple example for you. Type the following html in your cshtml file:

<form method="post">
    <button type="submit" class="btn btn-info"> Export</button>
    <input asp-for="Value1" type="hidden" value="1" />
    <input asp-for="Value2" type="hidden" value="2" />
</form>

The two inputs will be used in order to submit to the server values which will be used in order to filter your data. The following OnPost will return a static file but you can write your own code in order to return the filtered data and return them to the browser.

[BindProperty]
public int Value1 { get; set; }

[BindProperty]
public int Value2 { get; set; }

public ActionResult OnPost()
{
    // example of returning a file
    return File("~/Images/accounts.png", "image/png", "FileNameForBrowser.png");
}
0
votes

Thanks @pitaridis for your valuable inputs.

i ended up doing like this:

`    <a asp-page="./Index" asp-page-handler="Export"
    asp-route-param1 = "@Model.data1"
    asp-route-param2 = "@Model.data2"
    asp-route-param3 = "@Model.data3"
    class="btn btn-info">
    Export
    </a>
`

Page handler is OnGetExportAsync(...).On handler call I am getting page refresh, Not sure Why I have to again repopulate all the controls. Any idea on this ?

or other option could be to use the OnGetAsync() method & by passing a param that identify it as file download ?