Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 908
Kiran Avatar asked Nov 10 '13 02:11

Kiran


2 Answers

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.

like image 50
Tommy Avatar answered Nov 05 '22 20:11

Tommy


You have several options, like:

  • use custom html helpers (for short general html code);

  • use RenderPartial or RenderAction as it says here.

(Also you should consider using rooted pathes like '~/Views/someController/etc/to/my/partialView.cshtml' or even moving your code to some shared views '~/Views/Shared/....'. But that's all upon you).

EDIT:

Please, have a look at this article. It explains how exactly you should use mentioned methods. It must be rather in such way:

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

but

@Html.Partial("ViewName")
like image 36
Agat Avatar answered Nov 05 '22 20:11

Agat