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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With