开发者

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 shou开发者_JS百科ld not display anything unless I explicitly want to.

How can I do this?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜