服务器的发布操作结果后如何打开新选项卡 [英] How to open a new tab after server's Post Action Result

查看:60
本文介绍了服务器的发布操作结果后如何打开新选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是情况.

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

I have a save, and a print button :

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

但是打印按钮必须打开一个新标签页.如果只是我,我显然知道我必须在打印之前保存……这不是问题.我可以改用目标空白为该链接:

But the print button has to open a new tab. If it was just me, I obviously know I have to save before printing... it would not be an issue. I could use this link with target blank instead :

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

容易,但是现在一些用户认为打印按钮也应该保存页面.因为他们不推送保存...他们只是打印而模型更改丢失了,因为我无法在我的打印链接中调用post操作...这是一个链接.

Easy, but now some users think that the print button should ALSO save the page. Because they don't push save... they just print and the model changes are lost because I can't call the post action in my print link... it's a link.

起初,我以为可以对保存函数进行异步调用,但是我的模型太大了,它需要回传自己的动作(对吗?)

I thought, at first, that I could make an asynchronous call to a save fonction, but my model is way too big, it requires the post back of it's own action (right ?)

经历过:

如何在目标上使用Target = _blank response.redirect?

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

And i'm not sure if it really help in MVC... right now i'm stuck here :

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

推荐答案

首先,当用户单击打印"按钮时,我通过ajax请求发布数据,并在成功完成后打开一个新标签.

at first when user click on print button i post my data by ajax request and after successfully done i open a new tab.

示例:

$.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响应中编写类似示例的内容,然后可以进行类似操作

You want to write something like your example in http response then you can do something like

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

更新

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

I have added a full flow of a testing project below.

示例:

型号:

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

控制器:

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

Save.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>

这篇关于服务器的发布操作结果后如何打开新选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆