James
James

Reputation: 2999

Load controller's view in iframe without layout items

I have 2 separate solutions. I want to load one of project#1's content from its view in an iframe in project#2. The problem is inside the iframe, it also includes everything in its layout view (the header/footer,etc)

How can I load just the view? (while still using its controller) in the iframe?

Upvotes: 1

Views: 2015

Answers (3)

BesTuerzt
BesTuerzt

Reputation: 11

If you want to hidden the Layout completely, you can set the Layout to null like:

@{
    Layout = null;
} 

But if you want to hidden the Layout just in iframe, than:

@{
    if (Context.Request.Query["iframe"] == "true")
    {
        Layout = null;
    }
}

If your page dont founds the Context, than please:

@inject Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> HtmlHelper
@{
    var isIframe = HtmlHelper.ViewContext.HttpContext.Request.Query["iframe"] == "true";
    if (isIframe)
    {
        Layout = null;
    }
}

Upvotes: 1

Abdisamad Khalif
Abdisamad Khalif

Reputation: 825

In your _Layout view You can generate target link with your IFrame like:-

<li>@Html.ActionLink("Contact", "Contact", "Home", null, new {target ="MainiFrame"})</li>

and in your Content section (in _Layout view) like:-

<section class="content-wrapper main-content clear-fix">
                @RenderBody()
                <iframe name="MainiFrame" src="" width="920" height="1000" frameborder="0"></iframe>
            </section>

Then any view that you like to display in your IFrame you can write the following code in the upper corner of that view.

 @model YourProgramName.YourModelClass

    @{
        Layout = null;
        ViewBag.Title = "Your View Title";
    }

@* Rest of your codes and mark ups.......... *@

Telling that your view not to use the default Layout, other wise your view will load all of your _Layout elements like menus, banners etc.

Enjoy.

Upvotes: 1

VJAI
VJAI

Reputation: 32768

If you don't need the layouts then you have to return partial views from those controller actions.

Ex.

return PartialView("ViewName");

Upvotes: 1

Related Questions