In my application I have some basic user information that needs to be displayed on every page (name, profile img). At the moment I have simply set the model in the _Layout
I would create a BaseController
which retrieves the data in the Initialize()
override and sets it to a ViewBag
property. Now derive every Controller you create from BaseController
and in your layout use the ViewBag
property to access your user data.
public class BaseController : Controller
{
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
// retireve data
var data = new ApplicationBaseModel();
// set to viewbag
ViewBag.UserData = data;
}
}
This way you do not have to derive all your model classes from ApplicationBaseModel
. You can have strongly typed views and additionally your user data as a ViewBag
property.