-1

Is it possible to create a generic view with System.Reflection ?

So I could loop throught the class properties creating inputs with the @Html.EditorFor or @Html.Editor or even a normal <input />.

With this I could create a template and re-use it to save time and create a pleasant image to the users.

So is it possible?

Lucas
  • 1,259
  • 15
  • 25

1 Answers1

0

Yes, it is possible, the model of the view should be object, and the controller can be an generic controller, but you should build your own ControllerControllerFactory like:

    public class MyGenericControllerFactory : DefaultControllerFactory
    {
        public override IController CreateController(RequestContext reqContext, string controllerName)
        {
            if(is controlleName generic) //you should define how build controller name when is generic
            //it should contain the controller name and the generic type name
            {
                string contRealName = "";//get the real controller name from controllerName var
                string genTypeName = ""; //get the generic type name fomr controllerName var
                Type contType = Type.GetType(contRealName);
                Type genType = Type.GetType(genTypeName);
                contType = contType.MakeGenericType(genType);
                return an instance of contType from your IoC container
            }
            else
            {
                Type contType = Type.GetType(controllerName);
                return an instance of contType from your IoC container
            }
        }
    }
aperezfals
  • 1,341
  • 1
  • 10
  • 26
  • I used your answer and other things, i create a `.cshtml ` file or replace it, then i loop throught the `properties ` , if the property lead me to a `recursive loop ` i ignore it, if it is a implementation of `IEnumerable `, i apend a `@Html.DropdownList(propName)` and add a `ViewBag ` to the `ViewContext` with a auto generated linq query using AI, its like a big generic `StringBuilder` to generate and read a `.cshtml` file – Lucas Oct 13 '16 at 19:11