Shane
Shane

Reputation: 1216

Get Model Type from RazorView object

I have a System.Web.Mvc.RazorView object which is strongly typed when in cshtml.

Can I get the model type from an instance of this class?

Upvotes: 1

Views: 1408

Answers (2)

SLaks
SLaks

Reputation: 887857

This is possible.

Call BuildManager.GetCompiledType(view.ViewPath) to get the type generated by compiling the view.
You can find the model type by checking the generic argument of the compiled type's base type (which should be WebViewPage<TModel>)

Upvotes: 4

Darin Dimitrov
Darin Dimitrov

Reputation: 1039268

There's no way to get the model given only an instance of a System.Web.Mvc.RazorView. It's available inside the RenderView method which is passed a ViewContext but from the outside you can't access it. But if you are inside a view you could use the Model property.

Upvotes: 1

Related Questions