Tony
Tony

Reputation: 12715

ASP.NET MVC 3 RenderPartial / Razor and the iframe

I want to render a PartialView inside a <iframe src=''></iframe> The rendered partial view has its own JavaScript code and the CSS sheet. I tried two ways to get this work (none of them worked):

1)
    <iframe src="http://localhost:54351/Box/19"></iframe>

    public PartialViewResult Box(int id)
    {
        return PartialView(GetBox(id));
    }

Result: the plain text (string), there is no the CSS sheet and the JavaScript code doesn't work

====================================

2)

    <iframe src="@{ Html.RenderPartial("~/Views/Box.cshtml", @Model); }"></iframe>

Result: Obviously it's doesnt work, nothing shows inside the iframe

In the first solution, I was wondering if is it possible to return maybe a RazorView object (or something) which will have working JavaScript code and the CSS sheet. Any ideas ?

Upvotes: 3

Views: 17586

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93494

I think you are misunderstanding both partial views and iframes.

An iframe renders an entire web page, not just a partial one, inside of another web page. The iframe must have a complete URL that is a different page from the current page (if it was the same page, it would try to render the iframe within itself over and over creating an infinite loop).

What you need to do is specify the URL of a different action that returns a full view. If you render a partial view, there is no <head> tag, and therefore no script tags typically associated in a head tag. no <link> tags, no title, etc...

Upvotes: 6

James Allen
James Allen

Reputation: 819

I believe you need to return a full View rather than a PartialView. Partial views don't automatically pick up the layout (because they are designed to slot into an existing page). Since an IFrame is completely independant of the parent page, it will need it's own stylesheet and script reference tags.

    public ActionResult Box(int id)
    {
        return View(GetBox(id));
    }

Upvotes: 9

Related Questions