簡體   English   中英

打開帶有自定義屬性和模型的asp.net mvc窗口

[英]asp.net mvc window open with custom properties and model

我有一個帶有“提交”的表格,它將打開一個pdf文件

@using (Html.BeginForm(null, null, new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, htmlAttributes: new { @class = "custom" }))
  {
   ......
   @Html.TextboxFor(m=>m.Name)
   <input type="submit" value="Open File"/>
  }

我希望它在新窗口中打開文件。
-我需要將模型傳遞給post方法(solution1)
-我可以打開新窗口,但我也需要為其命名並在同一窗口中重新打開文件。 (解決方案2)

我有2種可能的解決方案,但我想結合它們的功能。

solution1:

@using (Html.BeginForm(null, null, new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, htmlAttributes: new { @class = "custom" }))
  {
   ......
   @Html.TextboxFor(m=>m.Name)
   <input type="submit" value="Open File" formtarget="_blank"/>
  }

solution2:

    @using (Html.BeginForm(null, null, new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, htmlAttributes: new { @class = "custom" }))
  {
    ......
    @Html.TextboxFor(m=>m.Name)
    <a href="#" onclick="fileDownload()" >Download File</a>
  }

<script type="text/javascript">
    function fileDownload() {

        var title = "my file";

        var win = window.open("@Url.Action("DisplayFile", "MyFile")?name=@Model.Name", title);
        win.onload = function () { this.document.title = title; };
    };
</script>

Solution2我不想將模型作為url參數傳遞,我希望它是POST調用。
Solution1我似乎無法自定義新窗口的title

兩種解決方案對我來說都很好,因此,如果有人可以找出其中一種解決方案,或者組合使用,我將使用它。

編輯:用示例模型值更新

您可以通過以下兩種方法進行操作。 首先,您可以使用解決方案1中的代碼,但可以向控制器添加參數以接受新頁面的標題。 可以通過表單上的隱藏字段(您可以通過javascript操作)設置該值。 然后,您可以在ViewBag上設置一個屬性,並在標題中輸出該屬性:

HTML:

<head>
    <title>@ViewBag.Title</title>
</head>

形成:

@using (Html.BeginForm(null, null, new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, htmlAttributes: new { @class = "custom" }))
{   
    @Html.TextBoxFor(m=>m.Name)
    @Html.Hidden("Title", "My File")
    <input type="submit" value="Open File" formtarget="_blank" />
}

控制器:

[HttpPost]
public ActionResult Index(SomeClass model, string title)
{
    ViewBag.Title = title;

    //do something profound..

    return View(model);
}

或者,您可以攔截submit並通過javascript自己執行發布,然后將結果手動寫入新窗口。 我在下面使用jQuery作為示例很容易,但是如果您不能/不想使用jQuery,則可以在純JavaScript中實現。 我給了表單一個ID,因此我可以訪問Submit事件:

形成:

@using (Html.BeginForm(null, null, new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, htmlAttributes: new { @class = "custom", id="myForm" }))
{
    @Html.TextBoxFor(m => m.Name)
    <input type="submit" value="Open File" />
}

腳本:

$(document).on('submit', '#myForm', function (e) {
    $.ajax({
        url: $(this).attr('action'),
        type: 'POST',
        data: $(this).serialize(),
        success: function (response) {
            var win = window.open("about:blank", "_blank");
            var title = "my file";

            win.onload = function () {
                this.document.write(response);
                this.document.title = title;
            };
        }
    });

    return false;
});

將PDF文件寫入瀏覽器非常棘手,尤其是跨瀏覽器。 您需要確保它具有正確的Content標頭,尤其是content-type ,它需要是application/pdf 您無法通過JavaScript進行設置,因此服務器端渲染是您的最佳選擇。 請注意,客戶端選項很少,但是它們不能在所有瀏覽器上正常工作。

其次,您的第一種解決方案很好,因為各種瀏覽器在顯示PDF文件時對窗口標題的處理方式不同。 現代瀏覽器讀取PDF 文檔的元數據並顯示Title屬性(如果有)。 如果沒有,它們只會顯示文件名。 如果舊的瀏覽器具有一個或臨時文件路徑,則它可能顯示文件名;如果沒有其他文件,則顯示其他文件名。

由於您沒有加載HTML文檔,因此document.title在這種情況下沒有意義。 這是默認的瀏覽器行為,您無法對其進行控制,至少不能以在所有瀏覽器之間都能正常工作的方式對其進行控制。

如果PDF標題不在您的控制范圍內,則可以通過設置來控制顯示的文件名(假設PDF沒有標題,或者由於某種原因,瀏覽器不會顯示它)

Content-Disposition: inline; filename=<your title.pdf>

在指定Content-Type之前需要指定此標頭,並且只能包含ASCII字符。

另一種選擇 (顯示自定義標題)是在目標頁面的iFrame中顯示PDF。 您可以刪除iframe的邊框,刪除body元素的填充/邊距,並調整iframe的高度/寬度以匹配窗口的高度/寬度,以使其看起來像僅顯示PDF。 請參閱此演示和相應的 (以在iFrame中顯示PDF,使其看起來就像在主窗口中加載的一樣)。

的HTML

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>Look ma - custom PDF title!</title>
</head>
<body>
  <div id=wrapper>
    <iframe src="http://www.cs.tut.fi/~jkorpela/latin9.pdf">
    </iframe>
  </div>
</body>
</html>

的CSS

body {
    margin: 0px;
    padding: 0px;
}

div#wrapper {
    position: fixed;
    width: 100%;
    height: 100%;
}

div#wrapper > iframe {
    display: block;
    width: 100%;
    height: 100%;
    border: none;
}

暫無
暫無

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

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