Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.RenderPartial giving me strange overload error?

I made a test partial page named _Test.cshtml and put it in the same directory as my view that will be calling it, here it is:

<div>hi</div> 

And in the calling cshtml view, I simply put:

@Html.RenderPartial("_Test") 

Which gives me the error:

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

I have also tried the full path with the same result.

I am very confused as to why this is acting this way, I assume I am missing something simple?

like image 541
naspinski Avatar asked Mar 25 '11 17:03

naspinski


People also ask

What is HTML 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.

What is the difference between RenderAction and RenderPartial?

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 HTML partial vs HTML RenderPartial & HTML action vs HTML RenderAction?

Render vs Action partialRenderPartial will render the view on the same controller. But RenderAction will execute the action method , then builds a model and returns a view result.

How do I use RenderPartial?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .


1 Answers

You are getting this error because Html.RenderXXX helpers return void - they have nothing to return because they are writing stuff directly* to response. You should use them like this:

@{ Html.RenderPartial("_Test"); } 

There is also Html.Partial helper, which will work with your syntax, but I'd not recommend using it unless you have to, because of performance (it first composes given partial view into string, and then parent view puts it into response*).

* this is not entirely true, they are actually being rendered into ViewContext.Writer and once whole page is rendered and composed, the whole thing goes to response

like image 77
Lukáš Novotný Avatar answered Sep 22 '22 22:09

Lukáš Novotný