MVC 5 – Refresh Parent Window After Executing Child Window

Close a windows tab upon returning a view:

Do this when a parent window opens a child window.

The child window does the processing, but after the processing, you want the parent window to refresh.

In the SAVE action of the controller of the view of child window put:

ViewBag.RefreshGrid = false; 

at the top of the action, and

ViewBag.RefreshGrid = true; 

Right before the return of the view.

Now, in the save view mentioned above, put this javascript logic below the Html.BeginForm:

if (ViewBag.RefreshGrid == true)
    {
        <script type="text/javascript">
            window.onload = function () {
                RefreshParent();
            };
        </script>
    }

And in the Javascript section, add the function:

<script type="text/javascript">
        function RefreshParent() {
            if (window.opener != null && !window.opener.closed) {
                window.opener.location.reload();
            }
        }
</script>

Please follow and like us: