2017-05-18 8 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

henユーザーが印刷ボタンをクリックすると、データがajaxリクエストでポストされ、正常に終了すると新しいタブが開きます。

例:HTTPレスポンスにあなたの例のようなものを書きたい

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

その後、あなたは私が

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

UPDATE

ような何かを行うことができますtestiのフルフローを追加しました以下のプロジェクト

例:

モデル:

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の2番目のメソッドから離れて、ちょうど私に汚い気持ちを与えてくれるでしょう:) – Tommy

関連する問題