2017-02-28 95 views
0

因此,在我的MVC網站上,我需要在新選項卡中打開動態生成的URL。到目前爲止我得到的代碼如下:在新選項卡中打開動態生成的鏈接

<a class='noline' onclick="location.href='<%:@Url.Action("GeneratePaymentUrl", "Home", new {target="_blank"})%>'"> Pay Invoices</a> 

    public ActionResult GeneratePaymentUrl() 
    { 
     try 
     { 
      string returl = GetPaymentUrl(); 

      if (returl.ToLower().StartsWith("http")) 
      { 
       //Response.Redirect(returl); 
       return new RedirectResult(returl, false); 
      } 
      else 
      { 
       return new RedirectResult("/"); 
      } 

     } 
     catch (Exception Ex) 
     { 
     } 
     return new RedirectResult("/"); 
    } 

現在,必須在點擊鏈接時生成URL;因爲它會轉到外部支付網關,授權時間極短。現在代碼有效;它將在哪裏打開正確的頁面,但不是在新標籤中。如果我嘗試將target="blank" href="/"添加到錨標記中;我最終在新窗口中獲取了登錄頁面,並在原始頁面中顯示了付款網關。

我如何得到這個在新窗口彈出?

回答

1

JavaScript: location.href to open in new window/tab?我想這裏就是答案。由於您正嘗試使用腳本加載頁面,因此目標屬性將被忽略。您必須爲要打開新頁面的腳本指定它。事情是這樣的:

<a class='noline' onclick="window.open('<%:@Url.Action("GeneratePaymentUrl", "Home", new {target="_blank"})%>,'_blank' 
);"> Pay Invoices</a> 

    public ActionResult GeneratePaymentUrl() 
    { 
     try 
     { 
      string returl = GetPaymentUrl(); 

      if (returl.ToLower().StartsWith("http")) 
      { 
       //Response.Redirect(returl); 
       return new RedirectResult(returl, false); 
      } 
      else 
      { 
       return new RedirectResult("/"); 
      } 

     } 
     catch (Exception Ex) 
     { 
     } 
     return new RedirectResult("/"); 
    } 

可能工作

1

您是否嘗試過使用window.open?

window.open("http://www.google.com")

〜一

+0

沒錯;而已。使用指定的_blank打開window.open。早晨在咖啡前張貼我的作品。謝謝! –

+0

不用擔心吉姆! –

相關問題