views:

4120

answers:

4

How can I have a view render a partial (user control) from a different folder? With preview 3 I used to call RenderUserControl with the complete path, but whith upgrading to preview 5 this is not possible anymore. Instead we got the RenderPartial method, but it's not offering me the functionality I'm looking for.

+17  A: 

I don't understand the issue...

Can you just do this?

<% Html.RenderPartial("~/Views/AnotherFolder/Messages.ascx", ViewData.Model.Successes); %>

If that isn't your issue, could you please include your code that used to work with the RenderUserControl?

Elijah Manor
I don't know why, but I just ignored the "Views" part for some reason.Thanks.
borisCallens
I wish we could just say /AnotherFolder/Messages
Simon_Weaver
A: 

I created an empty MVC project (preview 5)
Added a user controll to the Views/Account folder named myPartial.ascx
Then from Home/Index I tried one of the following combinations:

<%Html.RenderPartial("/Account/myPartial.ascx");%>

The following locations were searched: /Account/myPartial.ascx

<%Html.RenderPartial("~/Account/myPartial.ascx");%>

The following locations were searched: ~/Account/myPartial.ascx

<%Html.RenderPartial("~/Account/myPartial");%>

etc

Never was the partial found. Only moving the partial to /Shared/myPartial and then calling the following seems to work.

<%Html.RenderPartial("myPartial");%>

What am I missing?


Before this I had created my own helper class that had several extention RenderPartial methods to the Html class with or without controll name and viewdata. They would generate the relative path (through a pathhelper class) and call RenderUserControl with that path

borisCallens
+2  A: 

The VirtualPathProviderViewEngine, on which the WebFormsViewEngine is based, is supposed to support the "~" and "/" characters at the front of the path so your examples above should work.

I noticed your examples use the path "~/Account/myPartial.ascx", but you mentioned that your user control is in the Views/Account folder. Have you tried

<%Html.RenderPartial("~/Views/Account/myPartial.ascx");%>

or is that just a typo in your question?

anurse
+1  A: 

For a user control named myPartial.ascx located at Views/Account folder write like this:

<%Html.RenderPartial("~/Views/Account/myPartial.ascx");%>
Rahat