2016-10-11 13 views
-1

System.Reflectionで汎用ビューを作成することはできますか?

だから私はloopclass properties throughtでも@Html.EditorForまたは@Html.Editorまたは通常<input />inputsを作成することができます。

これで私はtemplateを作成し、それを再利用して時間を節約し、ユーザーに楽しいイメージを作成することができました。

汎用エディターテンプレート

これは可能ですか?

答えて

0

はい、それはビューのモデルが対象である必要があり、可能であり、コントローラは、汎用コントローラーすることができますが、同様にあなたがあなた自身の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 
      } 
     } 
    } 
+0

'.cshtml'ファイルを作成するか置き換えると、プロパティが'再帰的ループ 'につながる場合は 'プロパティ'をループします。 'IEnumerable 'の実装であれば無視します。 '@Html.DropdownList(propName)'を作成し、AIを使って自動的に生成されたlinqクエリで 'ViewContext'に' ViewBag 'を追加します。' .cshtml'ファイルを生成して読み込むための大きな一般的な 'StringBuilder' – Lucas