I would like to determine the view model type from a (strongly typed) ASP.NET MVC 4 View before the view is executed. My Controller logic enables me to determine the view name, and thus load the view programatically, however there doesn't appear to be anything to give a clue about the model type:
var res = ViewEngines.Engines.FindPartialView(this.ControllerContext, viewName);
if (res.View != null)
{
Type modelType = res.View.GetType(); //returns System.Web.Mvc.RazorView
//...so it would be great to be able to do:
modelType = res.View.GetModelType();//...but this does not exist
}
The reason I want to do this is because I am automatically mapping my domain models to view models - the request contains information from which I can derive the view name, but not the view model type, so I want to derive this from the view in order to do the model mapping.