berwyn
berwyn

Reputation: 986

ASP.NET MVC3 @Html.RenderPartial is throwing CS1502 Error

I'm building an MVC3 app for my dynamic web class, and while attempting to render a partial, I get the following error:

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

Now, the code I'm executing is this:

<div>
    <h2>Shipping Address</h2>
    @Html.RenderPartial("_AddressPartial");
</div>

Now, I've googled this, and from what I've seen, the answers are all for older versions of MVC and used the <% %> style syntax and got System.IO errors rather than the System.Web error I'm getting. I did follow their advice though and try with and without the semicolon, which made no difference as I still got the YSOD each time. Any ideas?

Upvotes: 11

Views: 6330

Answers (2)

gdoron
gdoron

Reputation: 150253

Notice that RenderPartial doesn't return any value (just like the RenderAction method), it writes the output on the request. while Partial (just like the Action method) return value of MvcHtmlString.
SO @Html... has to return some value while @{...some code...} doesn't have to.
In your case if you want to use renderPartial use it like that:

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

But why not use the Partial method, which you can use like that:

@Html.Partial("_AddressPartial")

Upvotes: 6

Marc Gravell
Marc Gravell

Reputation: 1062550

This might just be because RenderPartial doesn't return anything. Try either:

@Html.Partial("_AddressPartial")

or

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

Upvotes: 26

Related Questions