代码之家  ›  专栏  ›  技术社区  ›  Seth Petry-Johnson

在ASP.NETMVC中,为什么我不能从“MyCustomView”继承而不指定完整的类型名?

  •  7
  • Seth Petry-Johnson  · 技术社区  · 15 年前

    在我的MVC应用程序中,我通常声明一个基本视图类型,我的所有视图都继承自该类型。当我指定 Inherits="MyView" 在我的页面声明中,但如果我指定 Inherits="MyApp.Web.Views.MyView" .

    如果我指定一个强类型的视图名,效果很好 : Inherits="MyView<T> (其中T是任何有效类型)。

    我的基本视图类声明如下:

    namespace MyApp.Web.Views {
        public class MyView : MyView<object> {
        }
    
        public class MyView<TModel> : ViewPage<TModel> where TModel : class {
        }
    }
    

    更新: 注意,我 通过Web.config导入MyApp.Web.Views。如果我没有,那么强类型方法( Inherits="MyView<T>"

    2 回复  |  直到 15 年前
        1
  •  1
  •   Community CDub    8 年前

    从另一个帖子:

    在这里阅读更多- Generic Inherited ViewPage<> and new Property

        2
  •  1
  •   David Glenn    15 年前

    <namespaces> 元素,然后您应该能够使用 Inherits="MyView"

    <pages>
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="MyApp.Web.Views" />
      </namespaces>
    </pages>