在 MVC3 控制器操作中返回文件和呈现的视图 [英] return both a file and a rendered view in an MVC3 Controller action

查看:19
本文介绍了在 MVC3 控制器操作中返回文件和呈现的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过控制器方法调用既返回文件以供下载又更新视图?

is it possible to both return a file for download and update a view from a controller method call?

或者需要解决方法,也许使用 javascript(加载后调用页面刷新)?

or a workaround is needed, maybe using javascript (calling a refresh of the page after loading)?

文件下载是根据 ViewModel 中设置的参数动态创建的(从页面上的各种控件中搜索参数).

the file download is created on the fly along the parameters set in a ViewModel (search parameters from various controls on the page).

推荐答案

您可以使用 ViewModel 或 ViewData 从您的操作中返回文件的地址,并使用 window.location.href 如视图末尾的以下代码.

You can return the address of file using ViewModel or ViewData from your action, and use window.location.href as demonstrated in the following code at the end of your view.

控制器

public ActionResult Index()
{
    /* ... */

    ViewBag.FileName = "{FileName}";
    return View();
}

public ActionResult Download(string id) {
    /* ... */

    return File("{Path}", "{MIME type}", "{Desired file name}");
}

标记

<script type="text/javascript">

    window.location.href = "/home/download/" + "@Ajax.JavaScriptStringEncode(@ViewBag.FileName)";

</script>

这篇关于在 MVC3 控制器操作中返回文件和呈现的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆