Would someone confirm the best place for a partial view in ASP.NET MVC?
My thinkings are if it's a global view that's going to be used in many places then SHARED.
If it's part of a view that's been wrapped up into a partial view to make code reading easier, then it should go into the Views/Controller directory
Am I correct or am I missing something?
You can place the partial view in the Shared folder or you can store it under the controller-specific subfolder. It would to shared if used by multiple controllers and it would better go to the controller view folder if only used by a single controller.
A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.
To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.
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() .
I believe you are correct. Here is an example of something I do, general navigation partial views in my Shared directory. and then a partial views for a specific Controller in the Views/[ControllerName] Directory.
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