I have a question for the pros and cons on how reusable partial views should be used in a project.
In the first example, I have a layout that is used between all the views. In the layout, I have a partial view that gets called using Html.RenderAction("Index", "Header"). This header changes based on if the user is logged in or not and it renders on every view.
In the second example, I have a static layout that is used between all the views. However, in this layout there are no partial views being called. The Header partial view is being called on each view and basically does the same thing as the first example (changes based on if a user is logged in or not, etc.)
Which approach is better, is one way or another the correct way? Pros and cons of each?
One of the main ideas behind asp.net-mvc is to not reuse code. So with this in mind, you should have your
Headercode in your_Layoutfile. This way it is not being retyped in everyView, and if you needed to remove it or add route values, etc you do not have to update everyViewthat has it.An example of this is the
_LogOnPartialthat is in a default project. In the_Layoutit is called by@Html.Partial("_LogOnPartial"), and the_LogOnPartialview contains a logic statement that either displaysLogOn or RegisterorWelcome back....