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>

MVC 5 – How to Close a Browser Tab Using Javascript

Close a windows tab upon returning a view:

    @if (ViewBag.CloseWindow == true)
    {
    <script type="text/javascript">
        window.close();
    </script>
    }

 Closing a Windows tab via a button click while on view:

<button style="font-weight:bold; margin-top: 5px;" type="button" onclick="CloseTab();" class="btn btn-primary">Close Tab</button>

    <script>
        function CloseTab() {
            window.close();
        }
    </script>

MVC 5 – How to Display a PDF in a View

Assuming that you have a PDF in your controller as a byte array, add the following code to convert the data to a base64 string and send to your view.

string imageBase64Data = Convert.ToBase64String(results);
string imageDataURL = string.Format("data:application/pdf;base64,{0}", imageBase64Data);
ViewBag.ImageData = imageDataURL;

 In your view, display the PDF in an iframe.

 <div class="container">
    <div class="row">
        <div class="form-group-sm">
            <div class="col-md-12" style="padding-left: 0px;">
                <iframe style="width: 75%; height: 500px;" src="@ViewBag.ImageData"></iframe>
            </div>
        </div>
    </div>
</div>

MVC 5 – Using Javascript to set focus to control, textbox, button, dropdown list upon loading of a Razor View

Let’s say you want to add focus to a button in your view. The first thing you need to do is add the id attribute to your control and give it a name such as id=”RefreshListButton”.

Next, in the jquery document ready section, add a jquery function that will set the focus.

 $('#RefreshListButton').focus(); 

 $(document).ready(function () Here is the complete function:

  <script type='text/javascript'>
$(document).ready(function () {           
$('#RefreshListButton').focus();       
});
</script>