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.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- MVC-Routing,Why i can not ignore defaults,The matc
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Try using
RenderAction("myPartial","Account");
For a user control named myPartial.ascx located at Views/Account folder write like this:
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
or is that just a typo in your question?
I've created a workaround that seems to be working pretty well. I found the need to switch to the context of a different controller for action name lookup, view lookup, etc. To implement this, I created a new extension method for
HtmlHelper
:ControllerContextRegion
is defined as:The way this is used within a view is as follows:
There may be unwanted side effects for this if your code requires the
controller
route component to not change, but in our code so far, there doesn't seem to be any negatives to this approach.If you are using this other path a lot of the time you can fix this permanently without having to specify the path all of the time. By default, it is checking for partial views in the View folder and in the Shared folder. But say you want to add one.
Add a class to your Models folder:
Then in your Global.asax.cs file, add the following line:
Just include the path to the view, with the file extension.
Razor:
ASP.NET engine:
If that isn't your issue, could you please include your code that used to work with the RenderUserControl?