在MVC3控制器动作返回既是文件和渲染视图 [英] return both a file and a rendered view in an MVC3 Controller action

查看:99
本文介绍了在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)?

文件下载是在沿着一个视图模型的参数设置即时创建
(从各种控制搜索参数的页)。

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

推荐答案

您可以使用返回或视图模型从ViewData的你的行动文件的地址,并使用 window.location.href 作为您的视图的最后表现在以下code。

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天全站免登陆