ASP.NET MVC RenderPartial Compiler Error

Am I the only person who can stare at a piece of code for ten minutes and not notice the obvious?  I was trying to use the HtmlHelper.RenderPartial method tonight, using the code fragment below:

<%= Html.RenderPartial(
    "~/Views/Shared/ProductSummary.ascx", product) %> 

For those of you who are just getting started with a more recent version of ASP.NET MVC, the RenderPartial method replaces the RenderUserControl method in previous versions.  Anyway, I got this compiler error:

“Cannot implicitly convert type ‘void’ to ‘object’.”

It took me TEN minutes to figure out what was wrong…  The RenderPartal method does not return a string, but void.  So, I should have used the following code fragment:

<% Html.RenderPartial(
    "~/Views/Shared/ProductSummary.ascx", product); %>

So, if you get a similar compiler error and don’t see what is wrong, there are two differences, because the method returns void and not a string, as most HtmlHelper methods do:

  1. You have to use <% instead of <%=
  2. You have to put a ; after the closing ) of the RenderPartial statement

If you found this post helpful, please click below to “Kick” it:

kick it on DotNetKicks.com

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: