asp.net - 如何在服务器发布操作结果后打开新选项卡

标签 asp.net asp.net-mvc tabs response.redirect

情况是这样的。

我有一个保存和一个打印按钮:

<input name="btnSubmit" type="submit" value="Save" /> 
<input name="btnSubmit" type="submit" value="Print"/> @*Redirect to Action("Print", "controler")*@

但是打印按钮必须打开一个新选项卡。如果只是我,我显然知道我必须在打印之前保存......这不会是一个问题。我可以将此链接与目标空白一起使用:

<a target="_blank" href="@Url.Action("Print", "controler", new { id = Model.id })" type="submit" value="Print" > Print</a>

很简单,但现在有些用户认为打印按钮还应该保存页面。因为他们不推送保存...他们只是打印并且模型更改丢失,因为我无法在打印链接中调用后期操作...这是一个链接。

一开始我以为我可以对保存函数进行异步调用,但我的模型太大了,它需要回发自己的操作(对吗?)

经历过这个:

How do I use Target=_blank on a response.redirect?

而且我不确定它是否真的对 MVC 有帮助......现在我被困在这里:

[HttpPost]
public ActionResult MyForm(string btnSubmit, formModel model)
{
    if (btnSubmit == "Print")
    {
        dbSave(model);
        return RedirectToAction("Print", "controler"); // Won't open new tab... 
    }
}

最佳答案

首先,当用户单击打印按钮时,我通过 ajax 请求发布我的数据,成功完成后,我打开一个新选项卡。

示例:

$.ajax({
    url: "@Url.Action("create", "Post")",
    type: "POST",
    contentType: "application/json",
    data: JSON.stringify({ model: model})
}).done(function(result){
window.open('@Url.Action("Print", "controler", new { id = Model.id })', '_blank').focus();
}); 

或者

您想在http响应中编写类似示例的内容,那么您可以执行类似的操作

  HttpContext.Current.Response.Write( @"<script type='text/javascript' language='javascript'>window.open('page.html','_blank').focus();</script>");

更新

我在下面添加了一个测试项目的完整流程。

示例:

型号:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ProductCode { get; set; }
    public decimal Price { get; set; }
}

Controller :

 public class ProductController : Controller
    {
        // GET: Product
        public ActionResult Index()
        {
            return View();
        }


        // GET: Product/Create
        public ActionResult Save()
        {
            var model = new Product();
            return View(model);
        }

        // POST: Product/Create
        [HttpPost]
        public ActionResult Save(Product model, string saveButton)
        {
            if (ModelState.IsValid)
            {
                //do something 
                return
                    Json(
                        new
                        {
                            redirectTo = Url.Action("Index", "Product", new { Area = "" }),
                            OpenUrl = Url.Action("Print", "Product", new { Area = "" })

                        });
            }
            return View(model);
        }
        public ActionResult Print()
        {
            return View();
        }
}

保存.cshtml:

@model Product

@{
    ViewBag.Title = "Save";
}

<h2>Save</h2>
@Html.Hidden("saveButton","Test")@*Change Test to your value or change it to using JavaScript*@
@using (Html.BeginForm("Save", "Product", new {area = ""}, FormMethod.Post, new {id = "fileForm", name = "fileForm"}))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Product</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ProductCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ProductCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductCode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button type="button" class="btn btn-primary" id="btnSave">Save</button>
                <button type="button" class="btn btn-default">Print</button>
            </div>
        </div>
    </div>
}

脚本:

<script>
        $("#btnSave").click(function() {
            $.ajax({
                url: $("#fileForm").attr('action'),
                type: $("#fileForm").attr('method'),
                beforeSend: function() {
                },
                data: $("#fileForm").serialize() + "&saveButton=" + $("#saveButton").val()
            }).done(function(result) {
                if (result.OpenUrl) {
                    window.open(result.OpenUrl, '_blank');
                }
                if (result.redirectTo) {
                    setTimeout(function() {
                            window.location.href = result.redirectTo;
                        },2000);
                }


            });
        })

    </script>

关于asp.net - 如何在服务器发布操作结果后打开新选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44054886/

相关文章:

c# - 检查 request.form ["field"] 字符串长度在 ASP.net 中不起作用

asp.net - 如何在ASP.NET Core 1.0中获取bin文件夹

asp.net - GDI 抗锯齿功能在 Server 2008 上运行不佳

c# - 具有异步操作的 ASP.NET MVC

javascript - 谷歌地图无法读取未定义的属性 'maps'

asp.net-mvc - 在新的MVC 5中使用Bootstrap 3 RC1

java - 自定义 DialogFragment 中的 Tablayout 不显示选项卡文本和图标

css - 将水平 JQuery 选项卡更改为垂直选项卡

c# - 使用 ValidateRequest ="false"来规避 "A potentially dangerous Request.Form value was detected"的安全隐患是什么?

python - pyplot制表符