2017-05-18 29 views
2

以下是情況。如何在服務器發佈操作結果後打開新選項卡

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

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

容易的,但現在一些用戶認爲打印按鈕還應保存網頁。因爲他們不推送保存...他們只是打印和模型更改丟失,因爲我無法在我的打印鏈接中調用發佈操作...這是一個鏈接。

我想,首先,我可以對保存fonction異步調用,但我的模型是太大了,它需要它自己行動的回發(右?)

通過此去:

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

回答

3

在第1W母雞用戶點擊打印按鈕,我通過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(); 
}); 

OR

你想要寫的東西就像HTTP響應你的榜樣,那麼你可以這樣做

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

UPDATE

我已添加完整的testi流程下面的ng項目。

實施例:

型號:

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

你的第二個方法,它在MVC工作? –

+0

我喜歡用幾年來的東西。 – Ashiquzzaman

+0

@AntoinePelletier無法從控制器中打開一個新選項卡,這是一個UI關心的問題。因此,我認爲這個答案在AJAX文章的正確路徑上,但是應該實施一些額外的檢查(例如AJAX文章的成功)。另外,你可以(應該)直接在''標籤上放置一個點擊處理程序,然後你可以從點擊的對象中取出HREF,而不需要在JS中爲打印URL提供剃鬚刀標記。我會遠離MVC中的第二種方法,因爲它只是給我一種骯髒的感覺:) – Tommy

相關問題