95

This works, because it returns the result of partial view rendering in a string:

@Html.Partial("Path/to/my/partial/view")

But I prefer to use RenderPartial and it seems I need to write:

@{Html.RenderPartial("Path/to/my/partial/view");}

instead of:

@Html.RenderPartial("Path/to/my/partial/view");

To get it to work. Error message:

 Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

If there any better way instead of opening code block @{...} just for one method call?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
artvolk
  • 9,448
  • 11
  • 56
  • 85
  • What is the error that you get when you try to use `@Html.RenderPartial()` – Neil Knight Aug 08 '11 at 10:34
  • I've added message to my post. I understand why it shows an error @Html.RenderPartial() is parsed as roughly as <%?:@Html.RenderPartial() ?> which is wrong and not as Html.RenderPartial(); ?>, but I'm looking for a workaround. – artvolk Aug 08 '11 at 10:41
  • Related: [Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction](http://stackoverflow.com/questions/5248183/html-partial-vs-html-renderpartial-html-action-vs-html-renderaction). BTW why do you prefer to use `RenderPartial`? – Paolo Moretti Aug 08 '11 at 11:19

4 Answers4

143
  • RenderPartial() is a void method that writes to the response stream. A void method, in C#, needs a ; and hence must be enclosed by { }.

  • Partial() is a method that returns an MvcHtmlString. In Razor, You can call a property or a method that returns such a string with just a @ prefix to distinguish it from plain HTML you have on the page.

Ofer Zelig
  • 17,068
  • 9
  • 59
  • 93
  • 1
    Why do you prefer using Html.RenderPartial instead of Html.Partial? If you, from some reason, want/need to go with Html.RenderPartial, there is no workaround - that's the syntax. – Ofer Zelig Aug 08 '11 at 11:06
  • 5
    I use it because it should be more effective (because it doesn't return huge string). – artvolk Aug 08 '11 at 12:29
45

Html.RenderPartial() is a void method - you can check whether a method is a void method by placing your mouse over the call to RenderPartial in your code and you will see the text (extension) void HtmlHelper.RenderPartial...

Void methods require a semicolon at the end of the calling code.

In the Webforms view engine you would have encased your Html.RenderPartial() call within the bee stings <% %>

like so

<% Html.RenderPartial("Path/to/my/partial/view"); %>

when you are using the Razor view engine the equivalent is

@{Html.RenderPartial("Path/to/my/partial/view");}
Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
10
@Html.Partial("NameOfPartialView")
RouR
  • 6,136
  • 3
  • 34
  • 25
  • I'd like to use `Html.RenderPartial()`, that's why I asked this question. The details: "Performance-wise, it's been claimed that rendering directly to the output stream is better (which was why they went w/ the void RenderPartial to start with)." from here http://stackoverflow.com/a/2729851/118810 – artvolk Jun 15 '12 at 13:46
0

If you are given this format it takes like a link to another page or another link.partial view majorly used for renduring the html files from one place to another.

Sankar
  • 1,272
  • 2
  • 19
  • 31