Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor @{ ... } vs @ calling RenderPartial

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?

like image 560
Sachin Kainth Avatar asked Jul 03 '12 16:07

Sachin Kainth


People also ask

What is the difference between RenderPartial and RenderAction?

RenderPartial is used to display a reusable part of within the same controller and RenderAction render an action from any controller. They both render the Html and doesn't provide a String for output.

What is the difference between RenderPartial and partial?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.

What is the difference between Render () and RenderPartial ()? Write the syntax?

Renderpartial does exactly the same thing and is better for performance over partial(). The main difference between Partial and RenderPartial is : Partial return string and write it to document as Shweta said . RenderPartial actually write direct to context response. Html.

What is RenderPartial?

RenderPartial(HtmlHelper, String) Renders the specified partial view by using the specified HTML helper. RenderPartial(HtmlHelper, String, Object) Renders the specified partial view, passing it a copy of the current ViewDataDictionary object, but with the Model property set to the specified model.


1 Answers

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.

like image 109
danielQ Avatar answered Oct 04 '22 05:10

danielQ