【问题标题】:New window opens on click of actionLink单击 actionLink 会打开新窗口
【发布时间】:2011-12-13 20:02:41
【问题描述】:

我需要从视图页面调用控制器方法。单击下面的链接应调用该方法。

@Html.ActionLink(item.InvoiceNumber, "SendPdfStatement", "Invoice", 
                 new { item.InvoiceNumber }, new { target = "_blank" })

方法签名为:

public void SendPdfStatement(string InvoiceNumber)
    {

        InvoiceNumber = InvoiceNumber.Trim();

        ObjectParameter[] parameters = new ObjectParameter[1];
        parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);

        List<Models.Statement> list = new List<Models.Statement>();
        list = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters).ToList<Models.Statement>();

        var statementResult = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters);
        Models.Statement statement = statementResult.SingleOrDefault();

        pdfStatementController.WriteInTemplate(statement);                                 

   }

现在的问题是当我单击链接时,会打开一个空白窗口。我知道这与new { target = "_blank" } 有关。如果我在其位置传递null,我的链接页面将变为空白。我应该在这里传递什么,以便我的页面保持原样,并且不会出现新的空白窗口。

【问题讨论】:

  • 问题出在您的控制器方法中,而不是在此 html 帮助程序调用中。您的方法似乎返回一个空结果。您应该向我们展示缺少的代码。顺便说一句,您的方法应该在InvoiceController 类中调用SendPdfStatement。你确定你检查的是正确的吗?
  • 该方法仅在 InvoiceController 中调用 SendPdfStatement。已经放了方法的详细代码

标签: asp.net-mvc-3 c#-4.0 model-view-controller asp.net-mvc-5 asp.net-4.0


【解决方案1】:

试试这个

<%=Html.ActionLink(a.Title, "View", "Report", new { Id = a.id.ToString() }, new { target="_blank" })%> 

【讨论】:

    【解决方案2】:

    更改您的控制器操作。你得到的页面是空白的,因为你没有返回任何东西。做

     public ActionResult SendPdfStatement(string InvoiceNumber)
    
        {
        InvoiceNumber = InvoiceNumber.Trim();
    
            ObjectParameter[] parameters = new ObjectParameter[1];
            parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);
    
            List<Models.Statement> list = new List<Models.Statement>();
            list = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters).ToList<Models.Statement>();
    
            var statementResult = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters);
            Models.Statement statement = statementResult.SingleOrDefault();
    
            pdfStatementController.WriteInTemplate(statement); 
    
        return View();
        }
    

    编辑: 或者您应该使用 AJAX,这样您的页面就不会重新加载,并且您不必从您的方法中返回任何内容。阅读这里http://www.asp.net/mvc/overview/older-versions-1/contact-manager/iteration-7-add-ajax-functionality-cs

    【讨论】:

    • 但我没有任何东西可以返回这里
    • 你会的。您正在返回一个视图,以便可以在页面上显示某些内容。你可以做return View('pathhere');
    • 我的原始视图需要一个模型作为输入,该模型在“SendPdfStatement”中不可用
    • 查看我的编辑。您必须使用 AJAX,以免重新加载页面。然后您的链接将有 jquery 函数调用 'onclick="savePdf();"' 然后调用 HttpPost 控制器操作
    【解决方案3】:
    <input type="button" class="btn btn-primary" value="Text" onclick="window.open('@Url.Action("Action", "Controller")',target='_blank');return false;"/>         
    
     <button type="button" class="goBtn btn btn-primary btn-mid" onclick="window.open('@Url.Action("CheckInReport", "Staff")', target='_blank')" id="CheckIn">Check In</button>
    

    这也适用于 MVC5

    【讨论】:

      【解决方案4】:

      首先

      TARGET = "_blank"
      

      用于在新的浏览器窗口中打开超引用资源,所以如果您不想要新窗口 - 为什么要放置它? :-)

      其次,看看 ActionLink 辅助方法(我从下面引用的问题中得到的描述):

               Html.ActionLink(article.Title, 
                  "Item",   // <-- ActionMethod
                  "Login",  // <-- Controller Name.
                  new { article.ArticleID }, // <-- Route arguments.
                  null  // <-- htmlArguments .. which are none. You need this value
                        //     otherwise you call the WRONG method ...
                        //     (refer to comments, below).
                  )
      

      看看HTML.ActionLink method

      如果您遇到错误,我假设您遇到了错误的方法重载。如果您将 htmlArguments 替换为 null,您应该可以继续使用,但是当您的方法返回 VOID(无)时,您将得到一个空页面(您还期望什么 :)? )

      要取消默认导航机制,您可以实现简单的 jquery 规则:

      $('a.invoicelinkclass').click(function(e){
          e.preventDefault();
          $.get($(this).attr('href'),function(){
             // maybe an alert() or jquery ui .dialog() to let user know that something happened ?
          });
       });
      

      【讨论】:

      • 只想摆脱打开的空白页
      • 您可以使用 javascript(我已经更新了我的答案),但最好让用户知道他的操作是否成功
      【解决方案5】:
      @Html.ActionLink(item.InvoiceNumber, "SendPdfStatement", "Invoice", 
                       new { item.InvoiceNumber }, null);
      

      所以基本上最后一个参数是您的 html 属性,如果您不需要在新窗口中打开它...这是其中唯一的属性,所以只需为整个参数传递 null。

      【讨论】:

        【解决方案6】:

        尝试传递 _self 而不是 _blank 或传递 null 而不是 new { target = "_blank" }

        @Html.ActionLink(item.InvoiceNumber, "SendPdfStatement", "Invoice", new { item.InvoiceNumber }, null )

        或将new { target = "_blank" } 全部删除

        【讨论】:

        • 传递 _self 或 null 会使原始窗口变为空白。离开 { target = "_blank" } 会引发异常,因为 new { item.InvoiceNumber } 被用作 html 参数。
        猜你喜欢
        • 2018-01-23
        • 2015-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-12
        相关资源
        最近更新 更多