Is there a way to cycle through all the fields/properties of any given model and get both the name and the type? What is the "type" for a model? Every Controller inherits from Controller what do models inherit from? I know it is simply a class - how do you pass a type of class to a method?
Asked
Active
Viewed 41 times
0
-
Unless they explicitly inherit from a type they will inherit from `object`. There is no "base" type for models in general. – D Stanley Apr 21 '16 at 15:25
-
Sounds like you want to read up on the topic of `Reflection`. – Maximilian Gerhardt Apr 21 '16 at 15:26
-
@DStanley if I try to pass a model as an object to a method it does not contain properties or at least I don't know how to get them. – Dean.DePue Apr 21 '16 at 15:28
-
1You have 4 questions, at least one of which is a duplicate. – D Stanley Apr 21 '16 at 15:28
-
@MaximilianGerhardt - how do I use reflection to do what I want? – Dean.DePue Apr 21 '16 at 15:28
-
1@Dean.DePue Read the duplicate I marked. It should show you _exactly_ how to loop through properties without knowing the type. `GetType`, `GetProperties`, and `GetValue` are "reflection" methods. – D Stanley Apr 21 '16 at 15:29
-
AFAIK, in ASP.NET MVC, your models do not need to inherit from any specific class. You may leave it without any base class (in this case, the base class is object), or you may inherit form any class you want. – krimog Apr 21 '16 at 15:31