开发者

Hide iFrames Using the Controller

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 woul开发者_C百科d make converting the current actions simpler.

Thanks in advance!


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">

<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.


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.


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();
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜