View.cshtml
@using(Ajax.BeginForm("ReportData", new AjaxOptions {UpdateTargetId="result"})) { ... other input elements here <input type="submit" value="Display" /> <input type="submit" value="Export" onclick="this.form.submit();"> }
Controller.cs
public ActionResult ReportData(... parameters go here ...) { if (Request.IsAjaxRequest()) { // Do the normal behavior here to build up a string return Content(s); } // I actually stream the result with a custom ExcelResult action, // but this shows how to just return a file. return File(@"c:\library\report.xls", "application/vnd.ms-excel"); }
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.