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

Kiran
  • 2,754
  • 5
  • 26
  • 61

2 Answers2

27

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.

Tommy
  • 38,021
  • 8
  • 85
  • 116
  • 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 {}. Thanks – Kiran Nov 12 '13 at 11:01
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")
Community
  • 1
  • 1
Agat
  • 4,165
  • 30
  • 58
  • 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. – Kiran Nov 10 '13 at 13:15
  • I have used `@Html.Partial(...)` with no problems. Any exact reason you are using RenderPartial? – Siim Nov 10 '13 at 21:48
  • @Kiran Just added more info into the answer. – Agat Nov 10 '13 at 21:57
  • @Agat 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? – Kiran Nov 11 '13 at 02:47
  • 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. – Agat Nov 11 '13 at 02:58
  • @Agat : I have updated some more details on how the views are nested in my original post. - Regards – Kiran Nov 11 '13 at 13:39
  • @siim : no particular reason, I am just doing trial and error in various combinations to see if any one of that actually works. – Kiran Nov 11 '13 at 13:42
  • 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? – Agat Nov 11 '13 at 14:32