public class CheckMetadataAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
// get model
// get metadata for each model property
// set viewdata if metadata X exists
}
}
Old question was: How do I access ViewData within a custom ModelMetadataProvider? That was a no go.
You can’t/should not access ViewData or any HttpContext related info in a model metadata provider.
UPDATE:
After the updated question things start to make a little sense, so let’s update the answer:
This being said, obviously, using
ViewDatain an ASP.NET MVC application is something that I absolutely recommend against.ViewDatais weakly typed. Whatever you are trying to achieve (would have been nice by the way if you have explained what is your goal), don’t use ViewData, use view models and strongly typed views.Personally I am allergic to things like ViewBag/ViewData in ASP.NET MVC applications. When I do code reviews and see people using them I know they did something wrong.