mvc3 - using partial views in a different area

MoXplod picture MoXplod · Sep 10, 2011 · Viewed 26.6k times · Source

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)

Answer

Erik Funkenbusch picture Erik Funkenbusch · Sep 10, 2011

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)