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

2

11

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

Landsman answered 10/11, 2013 at 2:54 Comment(0)
E
31

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.

Effy answered 11/11, 2013 at 13:51 Comment(1)
You made my day! I had been struggling with this for last few days without understanding what was happening. issue was with the @ placed at the wrong location inside {}. ThanksLandsman
D
3

You have several options, like:

(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")
Dissociable answered 10/11, 2013 at 3:6 Comment(8)
I din't think i fully follow your comment. When I call these functions on a partial views that is in turn built from other partial views, I get the errors. I works if I copy post the 2nd level partial view in to the 1st partial view.Landsman
I have used @Html.Partial(...) with no problems. Any exact reason you are using RenderPartial?Overgrowth
@Landsman Just added more info into the answer.Dissociable
@Dissociable May be I was not clear, I don't have an issue with the syntax of these functions i have already followed same guidelines, issue is when I have one partial view that is build up of other partial views, and in that case when i try to render the topmost partial view, it throws error. is there a solution to this?Landsman
Well, I guess, you need to provide some more info of your hierarchy/structure, as embedded partial views just work generally, so there must be something specific in your situation.Dissociable
@Dissociable : I have updated some more details on how the views are nested in my original post. - RegardsLandsman
@siim : no particular reason, I am just doing trial and error in various combinations to see if any one of that actually works.Landsman
Ok. what's about the following: using this view name: ManageLoads.Mobile.cshtml without '.mobile'? Also, do the _MyWarehouse.cshtml have any model (or something) definitions in its header?Dissociable

© 2022 - 2024 — McMap. All rights reserved.