[英]Export file button with no page navigation in ASP.net MVC4
I am working on an ASP.net MVC4 web application and want to support import/export from/to excel feature. 我正在使用ASP.net MVC4 Web应用程序,并且希望支持从Excel功能导入/导出。 I have written the code(or actions) to export to excel but am having trouble in writing Import/Export buttons.
我已经编写了要导出到excel的代码(或操作),但是在编写“导入/导出”按钮时遇到了麻烦。
Specifically I just want buttons which only calls the export action and do nothing on current page. 具体来说,我只希望按钮仅调用导出动作,而在当前页面上不执行任何操作。
Code 码
localhost:52725/Excel/ExportToFile?pid=41本地主机:52725 / Excel中/ ExportToFile PID = 41?
Project Details page: 项目详细信息页面:
<div class="btn-group" id="footerButtons">
@Html.ActionLink("Edit Project", "Edit",
new { id = Model.ProjectId },
new { @class = "btn btn-primary " })
@using(Html.BeginForm("ExportToFile", "Excel"))
{
<button class="btn" title="Export to Excel"
type="submit">Export (Excel)</button>
}
@Ajax.ActionLink("Export (Excel)", "ExportToFile", "Excel",
new { pid = Model.ProjectId },
new AjaxOptions { HttpMethod = "Post",
UpdateTargetId="footerButtons", },
new { @class = "btn" })
<!-- Back to results button-->
</div>
Excel Controller Excel控制器
public class ExcelController : ControllerBase
{
public void ExportToFile(int pid = 0)
{
// Code to create and write to excel file
// Main point is it returns nothing which I speculate to be wrong.
}
}
You want the method signature to look like 您希望方法签名看起来像
public FileContentResult ExportToFile(int pid = 0)
and the output to use the Controller.File method 和输出使用Controller.File方法
something like 就像是
return File(fileBytes, "application/vnd.ms-excel", unique generated file name);
to return from the controller and send data eventually to the browser 从控制器返回并最终将数据发送到浏览器
as a simple test 作为一个简单的测试
return this.File(new System.Text.UTF8Encoding().GetBytes("my plain text content"),
"text/plain",
"my_file.txt");
should return a plain text file. 应该返回纯文本文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.