admin管理员组文章数量:1390948
I need to open a new tab after the controller success, the controller returns a View
This is my approach but it doesn't work:
function modify(){
var1= someDataFromDOM;
var2= anotherDataFromDOM;
$.ajax({
method: 'POST',
url: '@Url.Action("ModifyObject", "ControllerName")',
data: {id: var1, status: var2},
success: function (data){
var newTab = window.open("", "_blank", "", true);
newTab.document.body.innerHTML = data;
}
});
}
On the controller
[HttpPost]
public ActionResult ModifyObject(int id, string status)
{
ViewModelA model = new ViewModelA();
model = bd.GetModelA(id, status);
return View("ModifyObject", model);
}
The controller returns the view correctly but the newTab variable has null value
Any help will be wele
I need to open a new tab after the controller success, the controller returns a View
This is my approach but it doesn't work:
function modify(){
var1= someDataFromDOM;
var2= anotherDataFromDOM;
$.ajax({
method: 'POST',
url: '@Url.Action("ModifyObject", "ControllerName")',
data: {id: var1, status: var2},
success: function (data){
var newTab = window.open("", "_blank", "", true);
newTab.document.body.innerHTML = data;
}
});
}
On the controller
[HttpPost]
public ActionResult ModifyObject(int id, string status)
{
ViewModelA model = new ViewModelA();
model = bd.GetModelA(id, status);
return View("ModifyObject", model);
}
The controller returns the view correctly but the newTab variable has null value
Any help will be wele
Share Improve this question edited Jul 26, 2017 at 10:23 user3559349 asked Jul 26, 2017 at 7:15 dinadina 1111 gold badge2 silver badges9 bronze badges 4-
Is your controller name
ControllerController
? – ibubi Commented Jul 26, 2017 at 7:21 - No, @ibubi the name it's only an example but I just edited it. – dina Commented Jul 26, 2017 at 7:30
-
why don't you just open
@Url.Action("ModifyObject", "ControllerName")
in a new tab? The ajax is adding no benefit here, it's just ,making it more plicated – Liam Commented Jul 26, 2017 at 8:50 - And how can I do it that from js @Liam? – dina Commented Jul 26, 2017 at 10:05
1 Answer
Reset to default 4I think the problem is with the javascript window.open()
. This function is blocked by browsers except user events. See here and here
Below is a workaround for your purposes, I have tested;
<input type="button" hidden="hidden" id="hack" />
$(function () {
var var2 = "test";
var var1 = 1;
var htmlData;
var win;
$.ajax({
method: 'POST',
url: '@Url.Action("ModifyObject", "Controller")',
data: { id: var1, status: var2 },
success: function (data) {
htmlData = data;
$("#hack").trigger("click");
}
});
$("#hack").on("click", function () {
win = window.open("", "_blank");
win.document.body.innerHTML = htmlData;
});
});
However, opening a new tab like this may not be a good approach.
It is not apperant what your modify()
does, but I would not use ajax to open a new window, I would try to replace it with the below instead, please check here
Html.ActionLink("LinkText", "ModifyObject", "ControllerName", new { Id = "param1", Status = "param2" }, new { target = "_blank" });
Update
Try this as per your ment;
function modify()
{
var grid = $("#datagrid").data("kendoGrid");
var row = grid.getSelectedRow();
var win = window.open("","_blank")
var var1 = row.fieldID;
var var2 = row.fieldStatus;
var url = '@Url.Action("ModifyObject", "ControllerName")' + '?id=' + var1 + '&status=' + var2;
win.location = url;
}
本文标签: jqueryOpen a view in new tab from javascript when controller returnsStack Overflow
版权声明:本文标题:jquery - Open a view in new tab from javascript when controller returns - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744661621a2618280.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论