views:

358

answers:

3

Hi, I have tried finding the answer to this and come across several things which have not yielded the desired result.

So I wrote a HTML Helper that loads in an (fancybox) iFrame:
<%= Html.ActionFrame("Projects", "Edit") %>

The resulting page has a save button which currently saves the data and redirects to the Index page within the iFrame. As the action goes:

        if (ModelState.IsValid)
        {
            projectRepository.saveProject(record);
            return RedirectToAction("Index");
        }

But what I want to be able to do is that upon clicking the save the iFrame would close as well as saving the data.

I came across this, which is something similar that I would like to be able to do for the iFrames, it would make converting the current actions simpler.

Thanks in advance!

A: 

You could use jquery.post call your action from the iframe.

then use a callback from .post method to hide the iframe.

$.post('/MyController/MyAction',
  { param1: $('field1'), param2: $('field2') }, 
  function(data) {
  if (data) {
    $("#myIFrame").fadeout();
  }
}
SDReyes
A: 

Alright, I think I got this. I made a view called Close in the Shared directory and its content is:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Close</title>
</head>
<body>
    <div>
        <script type="text/javascript">
            parent.$.fn.fancybox.close();
            parent.location.reload(true);
        </script>
    </div>
</body>
</html>

And in the action I just go return View("Close");. It closes the iFrame and refreshes the page calling the iFrame.

aredkid
+1  A: 

I think returning a Close ActionMethod is your best bet. You can return JavaScript result from your ActionMethod but then you are defining how your html/javascript should act in your ActionMethod rather than in the View (which I don't particularly like).

Another solution which is identical to yours is...

<script type="text/javascript">
    if (window.parent != null)
        window.parent.location = '<%= Url.RouteUrl(new { 
            controller = "YourContoller",
            action = "YourAction" }) %>';
    else
        window.location = '<%= Url.RouteUrl(new { 
            controller = "YourController",
            action = "YourAction" }) %>';
</script>

You can then specifically redirect to a Controller/Action rather than just reloading the parent page. Or maybe you pass back a parameter/model that determines which route you should redirect to.

David Liddle
+1, True, this is a much nicer way of doing it, but due to lack of time I think I have implemented the method I had posted. Since I am just closing fancybox iFrames, I am only reloading the same controller and action and others are not involved.
aredkid