Html.RenderPartial () syntax using Razor

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

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

But I prefer to use RenderPartial , and I think I need to write:

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

instead:

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

To make it 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 it is best to open the @{...} code block for just one method call?

+80
asp.net-mvc asp.net-mvc-3 razor
Aug 08 2018-11-11T00:
source share
4 answers
  • RenderPartial() is a void method that writes to the response stream. The void method in C # requires a ; and therefore must be enclosed in { } .

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

+122
Aug 08 2018-11-11T00:
source share

Html.RenderPartial () is the void method - you can check if the method is the void method by hovering the 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 enclose your Html.RenderPartial () call within bee stings <%%>

So

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

when you use the Razor viewer, the equivalent

 @{Html.RenderPartial("Path/to/my/partial/view");} 
+41
Aug 08 '11 at 11:13
source share
 @Html.Partial("NameOfPartialView") 
+9
Jun 15 2018-12-12T00:
source share

If you are given this format, it accepts a link to another page or another link. A partial view that is commonly used to extract html files from one place to another.

0
Jan 17 '14 at 9:53 on
source share



All Articles