Razor @ {...} vs @call RenderPartial

I am trying to understand why, when I do this, I get an error

@Html.RenderPartial("MyPartial", Model); 

Compilation Error Description: An error occurred while compiling the resource required to service this request. Review the following error details and modify the source code accordingly.

Compiler Error Message: CS1502: Best Overloaded Method Compliance for "System.Web.WebPages.WebPageExecutingBase.Write (System.Web.WebPages.HelperResult)" contains some invalid arguments

But when I do this, partial rendering is fine

 @{ Html.RenderPartial("MyPartial", Model); } 

Does anyone know why the first example fails?

+6
source share
1 answer

It is basically the fact that this format ...

 @Html.RenderPartial("MyPartial", Model) 

... used for functions that do not return void , since RenderPartial returns void , you get this error.

Instead, in this block, it simply executes the code (which will internally execute the write call):

  @{ Html.RenderPartial("MyPartial", Model); } 

You can alternatively call

 @Html.Partial("MyPartial") 

... which returns a string.

+7
source

Source: https://habr.com/ru/post/919605/


All Articles