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:
- You have to use <% instead of <%=
- You have to put a ; after the closing ) of the RenderPartial statement
If you found this post helpful, please click below to “Kick” it: