3

In trying to get my brain around RenderPage v. Html.Partial v. Html.RenderPartial, I've been playing with some test files. I ran into some strange behavior: once RenderPage() is called, it seems like all subsequent calls to Html.RenderPartial() become no-ops. Why is one preventing the other?

Foo.cshtml:

<div>foo</div>

Bar.cshtml:

<div>bar</div>

Test1.cshtml:

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>
  @{ Html.RenderPartial("Foo"); }
  @RenderPage("Bar.cshtml")
</body>
</html>

Test2.cshtml:

// Test2.cshtml is identical to Test1.cshtml, except the two lines below
// ...
<body>
  @RenderPage("Bar.cshtml")        // this line used to be second
  @{ Html.RenderPartial("Foo"); }  // this line used to be first
</body>

Test1 behaves exactly as you'd expect:
foo
bar

However, Test2 never renders "foo"; it's as if my call to @{ Html.RenderPartial("Foo"); } never happens.

I realize this example is contrived - I'm not looking for ways to fix the issue. I'm trying to understand how RenderPage and Html.RenderPartial are related, and why they are interfering with each other.

mikemanne
  • 3,535
  • 1
  • 22
  • 30

1 Answers1

2

you can check this out

as mentioned by Annabelle:

Html.Partial("MyView")

Renders the "MyView" view to an MvcHtmlString. It follows the standard rules for view lookup (i.e. check current directory, then check the Shared directory).

Html.RenderPartial("MyView")

Does the same as Html.Partial(), except that it writes its output directly to the response stream. This is more efficient, because the view content is not buffered in memory. However, because the method does not return any output, @Html.RenderPartial("MyView") won't work. You have to wrap the call in a code block instead: @{Html.RenderPartial("MyView");}.

RenderPage("MyView.cshtml")

Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, like Html.RenderPartial(). However, it seems to always use the current view's model as the model for "MyView.cshtml".

and also by looking at here you can find out that : @RenderPage method of WebPageBase doesn’t use MVC template lookup and receives exact template path as its parameter

Community
  • 1
  • 1
Amir Jalali
  • 3,132
  • 4
  • 33
  • 46