Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dynamically get model name mvc 4 c#4

Using VS2012 and MVC4, how can I obtain the current model's name?

I want to dynamically name some variables using the model name itself. Is there a way? I have found controllers and actions, but not any model info, yet.

like image 941
Chad Hawley Avatar asked Apr 12 '13 01:04

Chad Hawley


1 Answers

Have you tried this?

@model.GetType().Name

As @Rowan Freeman notes in the comment below, you can also do this from the model itself:

GetType().Name; // or this.GetType().Name;

... or this from a controller:

var model = new MyCustomViewModel();
var modelName = model.GetType().Name;
like image 146
danludwig Avatar answered Sep 27 '22 23:09

danludwig