28

I have two questions regarding partial views...

  1. When to use Partial views vs @helper methods, i have used both interchangeably and would like to get more consistent in their usage. What do you guys do?

  2. How do you reference a partial view from another area.

I have an area called admin and i have a partial view in the regular Views directory. How do i use it .. i have tried the following which dont work as it cant be found.

@Html.Partial(VirtualPathUtility.ToAbsolute("~/Views/ControllerName/_PartialView"),
 Model)

other i have tried -

@Html.Partial("~/Views/ControllerName/_PartialView", Model)
MoXplod
  • 3,813
  • 6
  • 36
  • 46

5 Answers5

41

I'm not sure if you mean Html helpers, or razor helpers when you say "helpers" In any case, I only create Html helpers when it's a small, idividual item like a control.

If you mean Razor helpers, then they are different from Partials in that you can call them like functions, passing whatever parameters you want. Partials are largely stuck with the "model" system (and of course Temp/ViewData/Bag.

It's all about how you want to work with the code.

As for your Partial. You have to include the suffix.

@Html.Partial("~/Views/ControllerName/_PartialView.cshtml", Model)
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
38

Since the questioner asked about areas here's how to do it in an area

 @Html.Partial("~/Areas/Store/Views/Pages/Checkout.cshtml")
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • this seems to be necessary even within the area. I haven't ever had any luck with ../.. type relative paths but if there's a way to do that would be interested to hear – Simon_Weaver May 13 '13 at 23:15
  • 3
    Within the same area just "PrivacyPolicy.cshtml" will work if you are using the standard directory structure as MVC will search the expected directory structure. Relative paths aren't supposed to work AFAIK, probably cause it would be unclear what it is relative to due to the dynamic way MVC searches paths. I.e. would it start rooted in Area, or in Area/Views/ or /Area/Views/ControllerName/? So it's either filename only for something in the expected directory structure, or full path from app root such as in your answer. – AaronLS Jul 07 '14 at 20:24
  • I also have tweaked my default view finder (or whatever it's called) so it's possible I broke some default behavior along the way - but this way certainly works if you get stuck/frustrated – Simon_Weaver Sep 27 '14 at 17:58
3

I am just giving specific and simple example of what I'm trying to do. I need to be able to logoff from an area page using the partialview located in the main shared folder. Here's what I did:

  1. At the area view I reference the partial view by

       <div class="float-right">
            <section id="login">            
              **@Html.Partial("~/Views/Shared/_LoginPartial.cshtml")**
            </section>
       </div>
    
  2. At the Main shared folder where the _LoginPartial code was located I added {new = area ("")}, from:

    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    

    to:

    using (Html.BeginForm("LogOff", "Account", **new { area = "" },** FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    

Hope that helps in some way!

Taryn
  • 242,637
  • 56
  • 362
  • 405
leah
  • 31
  • 1
2

Another option is to make the partial view you want to share between areas SHARED.

So you put it in the main ~/Views/Shared/ folder, e.g.

~/Views/Shared/_MyPartialView.cshtml.

You can then refer to it from any area by saying

@Html.Partial("_MyPartialView")
Philip Johnson
  • 1,091
  • 10
  • 24
0

Make sure your Controllers in Areas have the [Area("MyArea")] annotation. As of this post, pulling in Partial Views from across Area boundries via Ajax div updates in ASP.NET Core works for me with Tag Helpers and @Html.ActionLink.

KramFfud
  • 179
  • 1
  • 2
  • 6