通用编辑器模板

Generic Editor Template

是否可以使用 System.Reflection 创建通用视图?

所以我可以 loop 通过 class properties 使用 @Html.EditorFor@Html.Editor 甚至正常的 <input /> 创建 inputs

有了这个,我可以创建一个 template 并重新使用它来节省时间并为用户创建一个令人愉快的图像。

那有可能吗?

是的,可以,视图的模型应该是对象,控制器可以是通用控制器,但是你应该构建自己的 ControllerControllerFactory,如:

    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
            }
        }
    }