2016-08-17 52 views
0

我試圖在新瀏覽器選項卡上打開一些內容。 我將數據從js ajax傳遞給MVC控制器,然後將查看結果返回給js,但將其作爲文本在新選項卡上打開 - 沒有html /源代碼只是簡單的文本。我如何將視圖返回爲Html?此外,我試圖不返回結果回JS(從JS刪除成功塊),並返回從MVC瀏覽器的權利,但它也不會工作。我究竟做錯了什麼 ?謝謝!查看以文本而不是html返回新瀏覽器選項卡(Ajax-MVC)

JS:

// open row data in new browser tab 
    function openInNewTab(e) { 

     var dataItem = this.dataItem($(e.currentTarget).closest("tr")); 
     console.log(dataItem); 
     var rowDetailsObject = new Object(); 
     rowDetailsObject.ID = dataItem.ID; 
     rowDetailsObject.Name = dataItem.Name; 
     rowDetailsObject.Number = dataItem.Number; 
     rowDetailsObject.Status = dataItem.Status; 


     $.ajax({ 
      type: "POST", 
      data: rowDetailsObject, 
      url: "/Home/NewTabPage", 
      dataType: "html", 
      success: function (result) { 

       var w = window.open(); 

       $(w.document.body).html(result); 
       console.log(result); 
      } 

     }); 
    }; 

MVC控制器:

public class HomeController : Controller { 

    public ActionResult NewTabPage(ContactList contactList) 
    { 
     ViewBag.ContactInfo = contactList; 


     return View(); 
    } 

} 

HTML:

@{ 
    { Layout = null; } 
} 

@{ 
    ViewBag.Title = "NewTabPage"; 
} 

<!DOCTYPE html> 
<html> 
<head> 
    <!-- CSS files references --> 
    <link href="~/Content/kendo.common.min.css" rel="stylesheet" /> 
    <link href="~/Content/kendo.default.min.css" rel="stylesheet" /> 
    <link href="~/Content/kendo.default.mobile.min.css" rel="stylesheet" /> 
    <link href="~/Content/ProjectCss.css" rel="stylesheet" /> 
    <!-- JS files references --> 
    <script src="~/Scripts/jquery-3.1.0.js"></script> 
    <script src="~/Scripts/kendo.all.min.js"></script> 
    <script src="~/Scripts/app/controller/OpenInNewTab.js"></script> 
</head> 
<body> 

<div> 
    <h2>Detailed data from Kendo row</h2> 


     <p>ID: <span id="ID">@ViewBag.ContactInfo.ID</span> </p> 

     <br/> 
     Name: @ViewBag.ContactInfo.Name 
     <br /> 
     Number: @ViewBag.ContactInfo.Number 
     <br /> 
     Status: @ViewBag.ContactInfo.Status 
</div> 
</body> 
</html> 
+0

檢查這個帖子:http://stackoverflow.com/questions/8919019/how-to-post-form-data-to-a-new-window-using-jquery-ajax –

回答

0

變化控制器PartialViewResult

public PartialViewResult NewTabPage(ContactList contactList) 
{ 
    ViewBag.ContactInfo = contactList; 

    return PartialView(); 
} 
相關問題