Question

Question: what am I missing to get this hack to work?

For the MVC app I'm working on, I've hit an 'unusual' case.

We generate certain reports using ActiveReports, and for users who are allowed, present an ActionLink on our layout that allows access to these reports, which open in a new window. The direction for this project is that all reports shall open in a new window.

However, there's one outlier that I can't apply the easy ActionLink fix to, because we access this report using an overlay that consists of pretty much a dropdown and a textfield. The combination of these is fed to a controller, which returns a redirect to an action based on the given conditions (there are some specific checks that should only occur on the server, thus why we don't just do this in JavaScript on this overlay dialogue.)

Now, I'm not one to let a little limitation like MVC not (directly) allowing me to Redirect to a new Window stop me, so I tried the given JavaScriptResult hack, because this is already a one-offish situation, and it seemed much the easiest and most maintainable solution to my problem, despite being a hack:

public ActionResult GenerateFooReport(string someInfo)
{
    // HACK: MVC does not allow redirects to open a new window, so
    //       return some JavaScript that forces a new window open.
    string script = "window.open('/controller/action?param=" + someInfo + "');";
    return JavaScript(script);
}

Of course, when I run the actions (input of Foo, the correct dropdown option) that lead to this method firing, I see the following output:

window.open('/controller/action?param=foo')

...and not the report that that piece of JavaScript is supposed to be automatically opening.

While I understand that this usage is (in most circumstances) considered an antipattern, I am not free to modify the UI to accomodate a more acceptable usage.

Question: what am I missing to get this hack to work?

EDIT: Attempted an alternate implementation which also did not work:

private ActionResult GenerateFooReport(string someInfo)
{
    // HACK: MVC does not allow redirects to open a new window, so
    //       return some JavaScript that forces a new window open.
    string script = "<script>window.open('/controller/action?param=" + someInfo + "');</script>";
    return Content(script);
}

This similarly had no effect.

EDIT: Another unsuccessful attempt at the hack:

private ActionResult GenerateFooReport(string someInfo)
{
    // HACK: MVC does not allow redirects to open a new window, so
    //       return a redirect to a url that executes javascript that
    //       opens the new window.
    string script = "javascript:window.open('/controller/action?param=" + someInfo + "');";
    return Redirect(script);
}

EDIT: The evil of this situation has had great influence! New direction has come to avoid these evil operations in the controller and instead do them in JavaScript. That being said, I am still curious to the actual way to get the hack to work, just for my own edification.

Was it helpful?

Solution

Try calling the jquery .load function directly instead.

return JavaScript("$('#targetdiv').load('controller/action?param="+someInfo+"');");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top