Hello guys,
Thank you for your help, I am starting to learn ASP.NET Core. :)
I started my web application without ASP.NET Core, but right now I prefer to upgrade to Visual Studio 2017 + Core.
I am facing one single problem: All my pages should inherit my header and footer views. And both of them have Models, and their Models aren't just text (for example, in the header I need to show notifications to the user).
In the current project - working like a charm - I have this BaseController:
public class BaseController : Controller { protected override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); var model = filterContext.Controller.ViewData.Model as BaseViewModel; if (model != null) { model.HeaderModel = this.getHeaderModel(); model.FooterModel = this.getFooterModel(); } } protected HeaderModel getHeaderModel() { HeaderModel model = new HeaderModel() { //... }; return model; } protected FooterModel getFooterModel() { FooterModel model = new FooterModel() { //... }; return model; } }
My BaseViewModel:
public class BaseViewModel { public HeaderModel HeaderModel { get; set; } public FooterModel FooterModel { get; set; } }
My Controllers inherit BaseController and return some View(model) [model inherit BaseViewModel].
With that in mind, I can populate my header and footer views.
@model Dashboard.Models.Dashboard.BaseViewModel<!DOCTYPE html><html lang="en"><head> ....</head><body class="nav-md"><div class="container body"><div class="main_container"> @{ Html.RenderPartial("_Header", Model.HeaderModel); } @RenderBody() @{ Html.RenderPartial("_Footer", Model.FooterModel); }</div></div></body></html>
In ASP.NET Core I can not use:
var model = filterContext.Controller.ViewData.Model as BaseViewModel;
What are your advice? Do you have any example I can follow?
Best regards,
Nelson.
EDIT: I am interested in following the best practises.