Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc3 - using partial views in a different area

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) 
like image 822
MoXplod Avatar asked Sep 09 '11 22:09

MoXplod


People also ask

When should partial views be used?

Partial views are an effective way to: Break up large markup files into smaller components. In a large, complex markup file composed of several logical pieces, there's an advantage to working with each piece isolated into a partial view.

How do I render a partial part of a view?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

Can we use multiple partial view in MVC?

You can only return one value from a function so you can't return multiple partials from one action method.

Can we use model in partial view?

Partial Views can use the Page Model for their data whereas Child Actions use independent data from the Controller. Editor/Display templates pass items from the model to the system but can be overridden by user partial views.


2 Answers

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) 
like image 194
Erik Funkenbusch Avatar answered Oct 13 '22 20:10

Erik Funkenbusch


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

 @Html.Partial("~/Areas/Store/Views/Pages/Checkout.cshtml") 
like image 36
Simon_Weaver Avatar answered Oct 13 '22 20:10

Simon_Weaver