Is there a filter or some other mechanism in ASP.NET MVC Core where I can evaluate the property and value of the model before the View is rendered? I'm annotating some properties of my model and I need to translate their value before the model is rendered in the View.
I tried using the 'IDisplayMetadataProvider' but this only works if the model properties are part of a model expression. In my case, they aren't -- they are often used just for display purposes (like the ViewBag.Title for instance).
Simple example:
public class MyModel
{
[Translate]
public string TitleKey { get; set; }
public string SomeOtherProp {get;set;}
public int AnotherProp {get;set;}
}
public class MyController
{
[HttpGet]
public IActionResult Index()
{
var vm = _service.GetViewModel();
vm.TitleKey = "Title.Translation.Key";
return View(vm);
}
}
Before the model is rendered in the View, I need to have some way of inspecting the model and finding which properties are annotated with "Translate". If they are, then get the value of that property and change it to something else. In this example I want to get the value of the "TitleKey" property, call a translation service to translate that value, then reassign that value before it hits the View.