Where should I edit BaseModelView (in BaseController)?
I have created a Base-Controller from which all the controllers inherit. Currently this controller fills some data (which I use in most views) into the ViewData
-Container like this:
protected override void Initialize(System.Web.Routing.RequestContext rc)
{
base.Initialize(rc);
ViewData["cms_configuration"] = new CmsConfiguration();
// etc.
}
I don't like the fact that I need to read (and cast) from ViewData within the views. I'd like to introduce a BaseViewModel from which all ViewModels will inherit from, defining the properties instead of using ViewData. But how or where can I populate the BaseViewModel within the BaseController? Is there some kind of hook? Or do I simply need to define a function in BaseController, which I call in the Child-Controller?
E.g. (Child-Controller:
//{...}
base.PopulateBaseView(MyView);
return View(MyView);
开发者_如何学运维Thx for any tipps. sl3dg3
You could optionally use ActionFilters to do stuff like this:
Check out this article:
http://www.asp.net/mvc/tutorials/understanding-action-filters-cs
It explains ActionFilters nicely. That way you can separate different populate-logic into different filters, and turn them on and off as you please.
精彩评论