Hello, I need to export some data to an excel document in an MVC application. I'm new to MVC and don't know how to react to the click event of the following button in the view.
<input type="button" value="@Resources.PortalAccountsMapping_ExportTitle" class="btnExport" />
How do I create an ActionResult that reacts when the button is clicked then exports data contained on the view inside a jqgrid table?
I have some code that does export the data I want, but there are some issues and I have a few questions.
1. When opening the excel file that is created I get this error:
The file format and extension don't match. The file could be corrupted or unsafe. unless you trust its source, don't open it. So you want to open it anyway?
How do I fix that? I need to export in .xlsx but if I change the file name with that extension in the export code the data does not populate the excel file.
2. Instead of the @Html.ActionLink("Export to Excel", "ExportToExcel") I would prefer a button <input type="button".
How do I do that and get the Controller to execute public void ExportToExcel?
3. I'm sure it is not best practice to have the excel export code in the Controller.
How can I move all that to the Model and still accomplish the export.
Open in new window