I’ve created a tag cloud partial view for my site. The partialview is to be included on all pages on the site. The data comes from a database. Is there a way to run the code that lives in the controller globally, so I don’t have to put it on every single action of every single controller?
I’d like to avoid putting this on every action:
public ActionResult Index()
{
ViewData["Tags"] = Tags.GetTags();
return View();
}
and
public ActionResult Index()
{
return View(Tags.GetTags());
}
It’d be a nightmare if I ever had to change that code. There has to be a better way of handling database bound content that is on every page of a site.
You could always use the
ViewModelpattern and have a baseViewModelclass for all your actions:Then just make every subsequent
ViewModelinherit this base class.And then in your
Controlleraction:Hope this helps.