How do you deal with avoiding ViewBag due to its risk of error with being dynamic but also avoid having to populate a new ViewModel and pass it back to the view each time. For instance, I don't want to necessarily change the follow to expose common data normally stuffed in ViewBag.
[HttpGet]
void Index()
{
return View();
}
to
[HttpGet]
void Index()
{
var messages = new MessageCollection();
messages.AddError("Uh oh!");
return View(messages);
}
Where in the pipeline would I add a property like ViewBag that is custom and strongly typed but have it exposed elegantly in the Controller and also the View. I'd rather do this when I don't need a specific ViewModel all the time...
[HttpGet]
void Index()
{
Messages.AddError("Uh oh!");
return View();
}
And on the view side, instead of @((IMessageCollection)ViewBag.Messages).Errors id rather have something like @Messages.Errors that is strongly typed and available everywhere. Also, I don't want to just cast it out in a code block at the top of my Razor view.
In WebForms, I would have done something like put this a base page and then have a usercontrol that can hidden or shown on pages as needed. With the Controller decoupled from the View, I'm not sure how to replicate similar behavior.
Is this possible or what is the best design approach?
Thanks, Scott