簡體   English   中英

服務器發布操作結果后如何打開新選項卡

[英]How to open a new tab after server's Post Action Result

這是情況。

我有一個保存和一個打印按鈕:

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

很簡單,但是現在有些用戶認為打印按鈕也應該保存頁面。 因為他們不推送保存...他們只是打印而模型更改丟失了,因為我無法在我的打印鏈接中調用post操作...這是一個鏈接。

我以為,起初我可以對保存功能進行異步調用,但是我的模型太大了,它需要回傳自己的動作(對嗎?)

通過這個:

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

而且我不確定它是否真的對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>");

UPDATE

我在下面添加了一個完整的測試項目流程。

例:

模型:

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>

暫無
暫無

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

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