ASP.NET MVC - Why doesn't my view inherits properly from System.Web.Mvc.ViewPage(of T)

风格不统一 提交于 2020-01-04 02:04:10

问题


It should be possible to use generics here and pass the class-type of the Model. However this is not accepted by Visual Studio 2008:

<%@ Page Inherits="System.Web.Mvc.ViewPage(of IEnumerable(of MyNamespace.MyClass))" %>

I get the following validation-error (underlined in VS): 'Context' is not a member of 'ASP._views___home___index__asp'

I get the following runtime error: BC30456: 'InitializeCulture' is not a member of 'ASP._views___home___index__asp'.

Note, I am using Visual Basic.NET - but that shouldn't impact the issue.

Here are some screenshots of the views & controllers, I hope somebody sees what's going on (TestClass is a simple class with ID and Name property):

  • View (list, model = IEnumerable)
  • View (index, no model)
  • Controller
  • Resulting page at runtime (list: error)
  • Resulting page at runtime (index: works)

The solution, as per Maik Koster answer below, is:

  1. Re-referencing System.Web.Mvc (had it in GAC, went back to local-copy)
  2. Re-created view from Controller. Right-click controller action -> Create view... and modify from there

Now the views in an ASP.NET MVC project the page directive has an inherits attribute as follows:

<%@ Page Inherits="System.Web.Mvc.ViewPage" %>

回答1:


I had the same problem and it was either caused by a missing reference, or was caused using a repository pattern and having implemented IDisposable.

For the latter scenario I added the namespaces: System.Data.Objects and System.Data.Objects.DataClasses. Next I added System.Data.Entity as a reference and also added it to the list of assemblies in web.config using

<add assembly="System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

After doing so this strange error disappeared. So you might want to check your references.




回答2:


Do it like this:

<%@ Page Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyNamespace.MyClass>>" %>



回答3:


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyProject.Helpers.PaginatedList<MyProject.Models.Person>>" %>

Hope you got.




回答4:


This might be a bug in visual studio intellisense. I have the same that inherited members are underlined in designer but I didn't got a runtime exception.




回答5:


I had the same error in MVC1. I change the namespace for my model and forgot to update the view. I updated the view and the errors went away.



来源:https://stackoverflow.com/questions/852585/asp-net-mvc-why-doesnt-my-view-inherits-properly-from-system-web-mvc-viewpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!