What is the best way to organize partial views in a file structure?

I will potentially have many Partial Views for my application, which can be grouped into a folder structure. It seems that I should do this, otherwise I will browse the folder with many files. Therefore, I assume that I should get something like:

Views -> Group1 -> PartialView1 PartialView2 

What does an HTML.partial call look like?

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

Another idea I had was to have one Partial View file with conditional blocks of code, but I suspect this is contrary to everything that PartialViews does.

Finally, is there a performance difference if you have many small partial views versus one large partial view with multiple conditional components? I think, I think, that one file is loaded into memory and compiled for code, and not for several small file downloads.

Thanks.

EDIT: additional information.

I have a general controller that I use to render various parts of the report, so all sections for the Introduction chapter will be displayed using the Introduction scores, i.e. "Introduction. Section 1", "Introduction. Section 2". In my scenario, I do not believe that I have common chapters, so I can go with the "file". but the Views folder will be large, so I am considering using subfolders.

EDIT: Thanks to everyone. There are some great ideas here. I went with the idea of ​​a folder at the end, since I use this approach elsewhere. However, I understand that I need to use the absolute path, but this is not a problem.

+6
source share
3 answers

As long as they are in the Views directory somewhere, that doesn't really matter. If you put it in a place other than Views/{controller} or Views/Shared , you will need the full location, including Views and extension, so @Html.Partial("~/Views/Group1/PartialView1.cshtml") .

Personally, if you have many partitions that are used in the same controller, I would leave them in the {controller-name} directory (with the main underscore, as suggested by @IyaTaisho). But if they are used in several controllers, and you need to group them, I would group them under Views/Shared/{groupName} .

Regarding one large versus many small particulars, I would say I will leave with many small ones. Sometimes there may be a reason to make one big, but in general I think that partial should be as simple as possible. Remember that you always have nested parts, therefore, if you have common functionality or a layout among many particles, you can break it into parent partial and many child parts below.

+3
source

You can use the naming convention for a child child, for example:

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

You can take one more step:

 contact.helpdesk.html contact.office.html 

Reusing partial data is much less common than unique partial data, so you can use the convention for reusable sequences, such as:

 global.partial1.html global.partial2.html 
  • Limitations - This is a large directory of files.

  • Benifits are easily removed, easily sorted.

+5
source

I usually add _ before partial. The example will have a basic look called Home.cshtml. The parts (partial) on the page will have something like this: _header.cshtml, _footer.cshtml, etc.

+5
source

All Articles