6

I'm trying to understand why when I do this in my view, I get an error

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

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

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

But when I do this, the partial renders fine

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

Does anyone know why the first example fails?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • 6
    Check this answer.. : http://stackoverflow.com/questions/5248183/html-partial-vs-html-renderpartial-html-action-vs-html-renderaction – Chandu Jul 03 '12 at 16:25
  • The one liner call is mostly to output data (such as @DateTime.Now) or pure C# logic (such as @model.ID != 0? yes : no). Block codes are for API processing, such as calling a partial view or other most sophisticated operations. Notice that the error states about writing a Helper to the page. – Andre Calil Jul 03 '12 at 16:30
  • Duplicate. This is essentially the same as question linked by @Chandu. – Alexei Levenkov Jul 03 '12 at 16:34
  • 2
    @AlexeiLevenkov: I'd say that it is similar but not identical. In terms of finding a question you might not think that something comparing partial and renderpartial has anything to do with why somethign needs to be in a block or not... – Chris Jul 03 '12 at 17:04

1 Answers1

7

It's basically the fact that this format...

@Html.RenderPartial("MyPartial", Model)

... is used for functions that don't return void, since RenderPartial does return void, you get that error.

Instead, in this block, it's just code execution (that will internally make the write call):

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

You can alternativelly call

@Html.Partial("MyPartial") 

... which does return the string.

danielQ
  • 2,016
  • 1
  • 15
  • 19