我的节点控制器:
Public Class NodeController Inherits System.Web.Mvc.Controller Function Display(ByVal id As Guid) As ActionResult Dim db As New WpfApplication1.Model1Entities Dim m As WpfApplication1.Node = (From n In db.Node _ Where n.Id = id _ Select n).First Return View(m) End Function End Class
当我运行项目并尝试导航到http://…/Node/Display/ [有效ID]时
我的显示操作出错:
Server Error in ‘/’ Application.
Compilation Error
Compiler Error Message: BC30456:
‘Title’ is not a member of
‘ASP.views_node_display_aspx’.Source Error:
Line 1: <%@ Page Title=”” Language=”VB” MasterPageFile=”~/Views/Shared/Site.Master” Inherits=”System.Web.Mvc.ViewPage(Of WpfApplication1.Node)” %>
Source File:
C:…\MvcApplication1\Views\Node\Display.aspx
我读过这个错误可能是由于a codebehind class naming conflict.但我认为这不是这种情况.
我是否可以不使用其他项目中的模型来创建强类型的ASP.NET MVC视图?
更新1:
我已经尝试在Display.aspx页面上导入命名空间,但没有
<%@ Import Namespace="WpfApplication1" %>
要么
<%@ Import Namespace="SolutionName.WpfApplication1" %>
要么
<%@ Import Namespace="SolutionName.WpfApplication1.Model1Entities" %>
防止错误.
更新2:
我已经尝试将命名空间添加到我的Views / Web.config文件中,但不是
<add namespace="SolutionName.WpfApplication1" />
也不
<add namespace="SolutionName.WpfApplication1.Model1Entities" />
防止错误.
更新3:
自从添加命名空间后,我现在收到此警告:
Namespace or type specified in the
Imports ‘SolutionName.WpfApplication1’
doesn’t contain any public member or
cannot be found. Make sure the
namespace or the type is defined and
contains at least one public member.
Make sure the imported element name
doesn’t use any aliases.
这是一个线索吗?
更新4:
我已将模型从我的WpfApplication1移动到新的ClassLibrary1.
我清理了控制器.
Imports ClassLibrary1 Public Class NodeController Inherits System.Web.Mvc.Controller Function Display(ByVal id As Guid) As ActionResult Dim db As New Model1Entities Dim m As Node = (From n In db.Node _ Where n.Id = id _ Select n).First Return View(m) End Function End Class
我清理了我的视图.
<%@ Import Namespace="ClassLibrary1" %> <%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of Node)" %>
我不再看警告.
但我仍然得到运行时错误.
解决方法
<system.web> <pages> <namespaces> <add namespace="SolutionName.WpfApplication1.Model1Entities" /> </namespaces> <pages> </system.web>
更新:
我没有任何WPF应用程序来测试它……我以为你试图使用“类库”项目.这肯定会奏效.是否有可能稍微重构您的应用程序以将“模型”拉出到他们自己的项目中?这样您就可以将其编译为类库.
更新2:
奇怪的是,这几乎就像你的页面没有正确地从System.Web.Mvc继承.确保您的Views / Web.config看起来像这样:
<?xml version="1.0"?> <configuration> <system.web> <httpHandlers> <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/> </httpHandlers> <pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter,System.Web.Mvc,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage,PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl,PublicKeyToken=31BF3856AD364E35"> <controls> <add assembly="System.Web.Mvc,PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" /> </controls> <namespaces> <add namespace="SolutionName.WpfApplication1.Model1Entities"/> </namespaces> </pages> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false"/> <handlers> <remove name="BlockViewHandler"/> <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/> </handlers> </system.webServer> </configuration>
更新3:运行时错误看起来不像是MVC项目.