Blankman
Blankman

Reputation: 266920

Display sidebar on main page only be default, other places can optionally override

I want my main page to display a side bar, and then for all other pages it should not display anything unless I explicitly want to.

How can I do this?

Upvotes: 0

Views: 226

Answers (2)

tvanfosson
tvanfosson

Reputation: 532435

I'd suggest using a common view model from which your other models derive. Store the information needed for the sidebar (or to load the sidebar via AJAX) in the common view model. Part of the model can also be a flag that indicates whether to show the sidebar or not. Have your layout strongly-typed to the common model and insert the sidebar into the HTML if the model indicates that it should be shown.

You can either populate the model in each action or in OnActionExecuted in your controller.

Upvotes: 1

itsmatt
itsmatt

Reputation: 31406

If you have in your _Layout.cshtml

@RenderSection("sidebar", required: false);

it will do what you want.

You can then display the sidebar section on any page where it makes sense but it won't be required to be there.

Upvotes: 2

Related Questions