return both a file and a rendered view in an MVC3 Controller action

后端 未结 3 1977
遥遥无期
遥遥无期 2020-12-17 03:36

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

or a workaround is needed, maybe using javascript (calling a refre

3条回答
  •  醉酒成梦
    2020-12-17 04:06

    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.

    Controller

    public ActionResult Index()
    {
        /* ... */
    
        ViewBag.FileName = "{FileName}";
        return View();
    }
    
    public ActionResult Download(string id) {
        /* ... */
    
        return File("{Path}", "{MIME type}", "{Desired file name}");
    }
    

    Markup

    
    

提交回复
热议问题