0

I would expect this to be asked/answered a hundred times, but the only answer I can find anywhere on the entire Internet is:

var currentModel = Model.GetType().Name;

This almost works but for example if the Model is defined as

@model Myproject.Model.User_info

Then the value of currentModel ends up as something like this:

User_info_23L7HGAFWLIUHI7GLIUBGFWAKHGI73I37GArwq

Do I really have to strip off everything starting with the penultimate underscore? Or is there a better way?

ohanaman
  • 35
  • 1
  • 8
  • `Model.GetType().Name` should work. Is your model an Entity model? – Jasen Jan 09 '18 at 00:13
  • Yes, but the string value comes out as pasted above with a GUID type of identifier appended with an underscore. – ohanaman Jan 09 '18 at 00:47
  • If you're working with the EF Proxy types then try [this answer](https://stackoverflow.com/a/16005340/2030565). The ObjectContext may now be in `System.Data.Entity.Core.Objects`. – Jasen Jan 09 '18 at 01:30

1 Answers1

0

Per Jasen's comment on the question above, the answer I ultimately went with was this:

var currentModel = System.Data.Entity.Core.Objects.ObjectContext.GetObjectType(Model.GetType()).Name;

... this reliably produces the correct name of both models and viewmodels as they are defined by the developer.

ohanaman
  • 35
  • 1
  • 8