【问题标题】:Using Response.Redirect in the View mvc4在视图 mvc4 中使用 Response.Redirect
【发布时间】:2014-10-22 09:19:40
【问题描述】:

我将按照以下步骤进行:

  1. 在控制器动作1号重定向到视图1;

  2. 在视图 1 中,我想显示 cshtml 页面,接下来我想使用

    重定向到新的操作 2
    @{Response.Redirect(Url.Action("CreatePdf", "Home");}
    

指令;

  1. 已执行第 2 项操作,我已获得结果(pdf 文件),但我无法看到我调用此操作的第 1 视图。 如何加载此视图并显示 html 页面?

【问题讨论】:

  • 重定向导致浏览器加载一个新页面,你期望发生什么?
  • 我不想加载新页面,而是触发新操作(并显示当前页面)
  • 您想在仍然显示页面的同时将 PDF 下载到客户端?
  • 是的,这就是我想做的事情

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

对@DavidG 的回答稍作调整:

<script type="text/javascript">
    $(document).ready(function () {
        setTimeout(DownloadPdf, 1000);
    });
    function DownloadPdf() {
        location.href = "@Url.Action("CreatePdf", "Home")";
    }
    </script>

刚刚测试和工作。 1秒后下载文件

【讨论】:

    【解决方案2】:

    重定向会导致整个会话被定向到新页面并丢失您发送的任何内容。我会改用 jQuery:

    <script type="text/javascript">
        $(document).ready(function () {
            setTimeout(DownloadPdf, 1000);
        });
    
        function DownloadPdf() {
            window.location = "@Url.Action("CreatePdf", "Home")";
        }
    </script>
    

    【讨论】:

    • 不幸的是,我现在可以看到但没有发生任何操作。
    • 还是一样,即使我加倍超时。
    【解决方案3】:
    I would suggest :
    
    public ActionResult ControllerAction1()
    {
        return View();
    }
    
    For the View(), for document.ready function : 
    $(document).ready(function () {
        $.ajax({
            url: '@Url.Action("Action2", "Controller")',
            contentType: 'application/json; charset=utf-8',
            type: 'POST',
            dataType: 'html',
            data: JSON.stringify(model)
        })
        .success(function(result) {
            // return true or false
            // html of json result
        })
        .error(function(xhr, status) {
    
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 2013-07-30
      • 2010-10-31
      • 1970-01-01
      相关资源
      最近更新 更多