jquery - Open a view in new tab from javascript when controller returns -
i need open new tab after controller success, controller returns view
this approach 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 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 view correctly newtab variable has null value
any welcome
i think problem javascript window.open()
. function blocked browsers except user events. see here , here
below workaround purposes, 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 new tab may not approach. not apperant modify()
does, not use ajax open new window, try replace below instead, please check here
html.actionlink("linktext", "modifyobject", "controllername", new { id = "param1", status = "param2" }, new { target = "_blank" });
update
try per comment;
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; }
Comments
Post a Comment