MVC4 Using nested @Html.RenderPartial() throws Compiler Error Message: CS1502

Kiran picture Kiran · Nov 10, 2013 · Viewed 18.4k times · Source

I am using MVC 4 and wanted to tidy up my views a bit so decided to create multiple partial views and bring them together during the rendering.

this works when the view being rendered has few @Html.RenderPartial('path\to\my\partialView.cshtml') but fails if this partialView.cshtml is in turn has further @Html.RenderPartial('path\to\my\otherPartialView.cshtml') defined inside it.

i get errors like with using either RenderPartial or Partial method

error CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
error CS1503: Argument 1: cannot convert from 'void' to 'System.Web.WebPages.HelperResult'

Is there a way in MVC4 we can achieve to tidy up my large view markup files? i.e. try to progressively compose the partial views with other partial views.

EDIT

Just to give more details.

My Mobile view looks like this:

File : ManageLoads.Mobile.cshtml

Location: Views->Shipper->ManageLoads

inside this view I have code like this:

<div id="landingPage" ng-show="MenuSelection=='DefaultPage'">
            @Html.Partial("~/Views/Shipper/_DashboardPartial.cshtml")
            <div class='message {{MessageClass}}'>
                <i class='{{MessageIcon}}'></i>
                <p>{{Message}}</p>
            </div>
        </div>

this part works fine without issue. now inside the _DashboardPartial.cshtml if I have reference to another partial view, it fails.

<div id="warehouseSelection" ng-show="getStep()==1">
   {@Html.RenderPartial("~/Views/Shipper/mobilePartials/_MyWarehouse.cshtml");}
</div>

this breaks and throws error, but if I copy paste the content of the "_MyWarehouse.cshtml" inside there, it starts to work again. I have verified that the path to the _MyWarehouse.cshtml is correct, so i suspected it has something to do with the nesting of the RenderPartial method that is causing the issue.

Regards Kiran

Answer

Tommy picture Tommy · Nov 11, 2013

The first issue that I see is that your Html.RenderPartial syntax is incorrect. The @ should be outside of the curly braces like so:

@{Html.RenderPartial("~/Views/Shipper/mobilePartials/_MyWarehouse.cshtml");}

Second, I wonder if the combination of Html.Partial and Html.RenderPartial is causing some issue here. Try using both Html.RenderPartial with the syntax above to see if that fixes your errorl.