簡體   English   中英

單擊鏈接時如何將參數傳遞到局部視圖?

[英]How to pass parameter to partial view on clicking Link?

嗨,我正在開發MVC5應用程序。 我有一個局部視圖,它基本上是彈出窗口。 我在點擊鏈接按鈕,如下所示。

window.open("/UploadDocument/ScannerUpload", "popupWindow", "width=1000,height=900,scrollbars=yes");

以下是我的ScannerUpload.cshtml文件。

<html>
<head>
    <script type="text/javascript">
        {
            function getParameterByName(name, url) {
                if (!url) url = window.location.href;
                name = name.replace(/[\[\]]/g, "\\$&");
                var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                    results = regex.exec(url);
                if (!results) return null;
                if (!results[2]) return '';
                return decodeURIComponent(results[2].replace(/\+/g, " "));
            }
            var foo = getParameterByName('Param1');
        }
    </script>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="X-UA-Compatible" content="IE=8" />  
    <title>ScannerView</title>
</head>
<body>
    <div>
        <iframe src="http://Scanner.aufytech.com/C3CKYC Scanner.xbap?" width="1000" height="600" scrolling="auto"></iframe>      
    </div>
</body>
</html>

我想向ScannerUpload.cshtml文件發送一些參數。 我可以知道有什么方法可以將參數附加到ScannerUpload.cshtml嗎? 任何幫助,將不勝感激。 謝謝。

因此,您需要做的第一件事就是弄清楚如何在首次調用中傳遞參數。 所以這:

window.open("/UploadDocument/ScannerUpload", "popupWindow", 
"width=1000,height=900,scrollbars=yes");

需要成為這個:

window.open("/UploadDocument/ScannerUpload?Param1=A&Param2=B", "popupWindow", 
"width=1000,height=900,scrollbars=yes");

您如何做取決於您,因為我不知道這些參數來自何處。 如果它們來自html頁面上的字段,則需要使用javascript事件來動態更新window.open

現在,您需要在第二個窗口中選擇這些參數。 回想起來,我意識到我們不需要解析參數就可以獲取URL的末尾。

因此,請大量參考以下內容: 動態設置iframe src iFrame onload JavaScript事件

這樣的事情可能會起作用(不好意思,抱歉):

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="X-UA-Compatible" content="IE=8" />  
    <title>ScannerView</title>
</head>
<body>
    <div>
        <iframe id="myIframe" src="" width="1000" height="600" scrolling="auto" onLoad="fUpdateiFrame();"></iframe>      
    </div>

    <script type="text/javascript">
            function fUpdateiFrame() {
                if (!url) url = window.location.href;
                document.getElementById('myIframe').src = "http://Scanner.aufytech.com/C3CKYC Scanner.xbap?" + url.split("?")[1];
            }
    </script>

</body>
</html>

所做的更改:

  1. 將ID添加到iframe元素,以便我們可以在javascript中引用它
  2. 將事件添加到iframe元素,以便在就緒時調用javascript函數
  3. 在設置新的iframe src值之前,添加一些可將查詢參數(在?之后的所有內容)切掉的javascript並將其添加到網址中

如果這第一次生效,我會感到驚訝-請發回任何問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM