Render partial from different folder (not shared)

181,470

Solution 1

Just include the path to the view, with the file extension.

Razor:

@Html.Partial("~/Views/AnotherFolder/Messages.cshtml", ViewData.Model.Successes)

ASP.NET engine:

<% 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?

Solution 2

In my case I was using MvcMailer (https://github.com/smsohan/MvcMailer) and wanted to access a partial view from another folder, that wasn't in "Shared." The above solutions didn't work, but using a relative path did.

@Html.Partial("../MyViewFolder/Partials/_PartialView", Model.MyObject)

Solution 3

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:

public class NewViewEngine : RazorViewEngine {

   private static readonly string[] NEW_PARTIAL_VIEW_FORMATS = new[] {
      "~/Views/Foo/{0}.cshtml",
      "~/Views/Shared/Bar/{0}.cshtml"
   };

   public NewViewEngine() {
      // Keep existing locations in sync
      base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NEW_PARTIAL_VIEW_FORMATS).ToArray();
   }
}

Then in your Global.asax.cs file, add the following line:

ViewEngines.Engines.Add(new NewViewEngine());

Solution 4

For readers using ASP.NET Core 2.1 or later and wanting to use Partial Tag Helper syntax, try this:

<partial name="~/Views/Folder/_PartialName.cshtml" />

The tilde (~) is optional.

The information at https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-3.1#partial-tag-helper is helpful too.

Solution 5

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

<%Html.RenderPartial("~/Views/Account/myPartial.ascx");%>
Share:
181,470

Related videos on Youtube

Boris Callens
Author by

Boris Callens

Senior .net programmer. Belgium(Antwerp) based. linked-in My real email is gmail.

Updated on April 16, 2020

Comments

  • Boris Callens
    Boris Callens about 4 years

    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.

  • aruno
    aruno about 15 years
    I wish we could just say /AnotherFolder/Messages
  • Arnis Lapsa
    Arnis Lapsa about 13 years
    @Simon_Weaver You can achieve that. One way would be to modify ViewEngine and override it's FindPartialView method with something like if(partialViewName.Contains"/")partialViewName="~/Views/"+pa‌​rtialViewName;
  • Chris
    Chris about 12 years
    Works in the MVC 3 Razor engine as well, but like the above, you need the extension (.cshtml).
  • Curtis Yallop
    Curtis Yallop almost 11 years
    Similarly @Html.Partial("../Shared/_PartialView") to use the Shared folder.
  • Pamma
    Pamma over 10 years
    Please Read the question as the user is asking about view in different folder where is folder in your code ?
  • Luke
    Luke over 9 years
    I'm finding that this doesn't work without the .cshtml extension at the end.
  • Paul
    Paul about 9 years
    Of course I realize this question was asked a long time ago. Thought I'd add to it for future Googlers and future Bingers.
  • sandeep talabathula
    sandeep talabathula about 9 years
    If its under different are you would need to give path "~/Areas/TestArea/Views/Shared/_SomePartial.mobile.cshtml"
  • jasdefer
    jasdefer over 6 years
    How do you handle different cultures of that partial view (for example ~/Views/AnotherFolder/Messages.en.cshtml)?
  • Demian Berisford-Maynard
    Demian Berisford-Maynard almost 5 years
    - Won't work in .Net Core 2.2, as RazorViewEngine.PartialViewLocationFormats doesn't exist.