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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With