7

I will potentially have many Partial Views for my application which can be grouped in a folder structure. It seems I ought to do this otherwise I will a View Folder with loads of files. So I assume I should have something like:

Views -> 
  Group1 -> 
    PartialView1
    PartialView2

What would the HTML.Partial call look like?

 HTML.Partial("~/Views/Group1/MyPartialView.cshtml",Model)

Another idea I had was to have the one Partial View file with conditionals Code blocks, but I suspect this goes against everything that PartialViews are about.

Finally is there any difference in performance if one has many small Partial Views versus one large Partial View with multiple conditional components? I guess I am thinking that one file load into memory and compilation to code as opposed to multiple small file loads.

Thanks.

EDIT: More Info.

I have a generic controller that I am using to render different parts of a report, so all sections for an "introduction" chapter would be rendered using "Introduction" partials ie "Introduction.Section1", "Introduction.Section2". In my scenario I do not believe I have common sections across chapters, so I could go with the "file." idea, but the Views folder would be large, hence why I am considering the use of subfolders.

EDIT: Thanks all. Some tremendous ideas here. I went with the folder idea in the end since I use this approach elsewhere. However I do realise I need to use absolute pathing, but this is not an issue.

SamJolly
  • 6,347
  • 13
  • 59
  • 125
  • 1
    Have you looked into using Editor/Display templates per actual model type? it allows you to link a view to a model type in a system-wide fashion without manually having to provide the names of the partial views. (http://stackoverflow.com/a/5497234/1373170) – Pablo Romeo Apr 16 '13 at 15:49

3 Answers3

6

You could use a parent child file naming convention like:

header.html
header.login.html
header.searchbar.html

You could even take it a step further:

contact.helpdesk.html
contact.office.html

Re-using partials is much less frequent than unique partials, so you could use a convention for re-usable partials like:

global.partial1.html
global.partial2.html
  • Limitations are a large file directory.

  • Benifits are easy to skim, easy to sort.

Dan Kanze
  • 18,485
  • 28
  • 81
  • 134
  • Interesting idea. In terms of MVC3, do you think this is preferable to having folders for the same purpose ie Header\header.html, Header\Login.html, Header\SearchBar.html – SamJolly Apr 16 '13 at 14:37
  • 1
    @SamJolly You might lose your mind. Also, the parent in this case is not a folder but the file itself. The partial to the parent that is. – Dan Kanze Apr 16 '13 at 14:43
  • 1
    @Monkieboy If you have a very large web app your file directory will be large, however it will be easily skimmable. That is, all of the parents will be in blocks, and the partials will be alphabetical. – Dan Kanze Apr 16 '13 at 14:45
  • 2
    It's interesting, but it looks like it ties your partial view explicitly to the page view (even though it doesn't). So if you have a partial that needs to be used by two different views (let's say `ProductPricingInfo` used by both `ProductList` and `ProductDetails`), what would you name the partial? In the end, it's just a name, so you could name it whatever you want and still use it anywhere, but a convention like this gives you the appearance that you're limiting it. – Joe Enos Apr 16 '13 at 14:58
  • 1
    @JoeEnos That's a great point. I think mabye in that case you could use a `global.name.html` convention. Re-using partials is much less frequent than unique partials. – Dan Kanze Apr 16 '13 at 15:35
6

As long as they're in the Views directory somewhere, it shouldn't really matter. If you put it in a location other than Views/{controller} or Views/Shared, then you'll need the fully qualified location, including Views and the extension, so @Html.Partial("~/Views/Group1/PartialView1.cshtml").

Personally, if you have a lot of partials that are used in a single controller, I'd leave them in the {controller-name} directory (with a leading underscore as @IyaTaisho suggested). But if they're used by in multiple controllers, and you need to group them, I'd group them under Views/Shared/{groupName}.

Regarding one big vs. many small partials, I'd say go with many small ones. There might be a reason to do one big one now and then, but in general, I believe a partial should be as simple as possible. Remember you can always have nested partials, so if you have shared functionality or layout among many partials, you can break it into a parent partial and many child partials underneath.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • Thanks for this. Actually I have a generic controller that I am using to render different parts of a report, so all sections for an "introduction" chapter would be rendered using "Introduction" partials ie "Introduction.Section1", "Introduction.Section2". In my scenario I do not believe I have common sections across chapters, so I could go with the "file." idea, but the Views folder would be large, hence why I am considering the use of subfolders. – SamJolly Apr 16 '13 at 15:29
5

I usually add an _ in front of a partial. Example would be have a main View called Home.cshtml. The pieces (partials) on the page would have something like this: _header.cshtml, _footer.cshtml, etc.

IyaTaisho
  • 863
  • 19
  • 42
  • Thanks for this. Yes aware of the "_" approach. Problem is not to distinguish Partials, but how to organise the large number of different Partials. – SamJolly Apr 16 '13 at 14:38
  • Ah, I see. I have a similar issue. I inherited a massive amount of them in my current project. For now, I've let it be as is because it was easier to deal with (mixing partials and views). I would almost suggest making a folder for them but at the same time I'm not sure. Sorry I can't be anymore help than that. – IyaTaisho Apr 16 '13 at 18:21