c# - 使用自定义属性和模型打开 asp.net mvc 窗口

标签 c# javascript asp.net asp.net-mvc 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 方法(解决方案1)
- 我可以打开新窗口,但我还需要为其命名并在同一窗口中重新打开文件。 (解决方案2)

我有 2 种可能的解决方案,但我想结合它们的功能。

解决方案1:

@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"/>
  }

解决方案2:

    @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 中的代码,但向 Controller 添加一个参数以接受新页面的标题。该值可以通过表单上的隐藏字段进行设置(您可以通过 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" />
}

Controller :

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

    //do something profound..

    return View(model);
}

或者,您可以拦截submit并通过javascript自行执行发布,然后手动将结果写入新窗口。为了方便起见,我在下面使用了 jQuery 作为示例,但是如果您不能/不想使用 jQuery,那么这在纯 JavaScript 中是可能的。我已经为表单提供了一个 ID,以便我可以访问提交事件:

表格:

@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;
});

关于c# - 使用自定义属性和模型打开 asp.net mvc 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25627004/

相关文章:

javascript - 设置 React useReducer 的最佳方法

asp.net - 免费使用基于文档的数据库

javascript - 在 React 组件之间使用 mui 图标

javascript - jQuery Post 循环,导致浏览器崩溃

c# - 尝试使用 asp.net 更新数据库时出现异常错误

c# - Asp.net Request.Browser.Crawler - 动态爬虫列表?

c# - C#-OutOfMemoryException将列表保存在JSON文件中

c# - Telerik 网格工具栏插入 - 显示自定义文本

c# - "File does not begin with ' %PDF- '."尝试加载 PDF 时出错 - ASP.NET MVC

c# - 代码优先自引用外键(多个)