ASP.NET MVC视图可以使用来自不同项目的模型吗? [英] Can an ASP.NET MVC View use a Model from a different project?

查看:92
本文介绍了ASP.NET MVC视图可以使用来自不同项目的模型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF项目中有一个名为Node的ADO.NET实体框架类。我想在同一个解决方案中的ASP.NET MVC项目中使用它。



我的节点控制器:

 
公共类NodeController
继承System.Web.Mvc.Controller

函数显示(ByVal id as Guid)作为ActionResult
Dim db As New WpfApplication1.Model1Entities
Dim m As WpfApplication1.Node =(From n in db.Node _
其中n.Id = id _
选择n).First
返回视图(m )
结束函数

结束类

当我运行该项目并尝试导航到 http://.../Node/Display/ [一个有效的ID]



我收到一个错误显示操作:


'/'应用程序中的服务器错误。



编译错误



编译器错误消息:BC30456:
'标题'不是
'AS的成员



第1行:<%@页面标题= Language =VBMasterPageFile =〜/ Views / Shared / Site.MasterInherits =System.Web.Mvc.ViewPage(Of WpfApplication1.Node)%>



源文件:
C:... \MvcApplication1\Views\Node\Display.aspx


我读过这个错误可能是由于 codebehind类命名冲突。但是我不认为是这样的情况。



我不能使用不同项目的Model来创建一个强类型的ASP.NET MVC View? p>

更新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 :



自添加命名空间后,我现在得到这个警告:



中指定的命名空间或类型Imports'SolutionName.WpfApplication1'
不包含任何公共成员或
无法找到。确保
命名空间或类型被定义,
至少包含一个公共成员。
确保导入的元素名称
不使用任何别名。


这是一个线索? / p>

更新4:



我将模型从我的WpfApplication1移动到了一个新的ClassLibrary1。

我已经清理了我的控制器。

 
导入ClassLibrary1

公共类NodeController
继承System.Web.Mvc.Controller

函数显示(ByVal id as Guid)作为ActionResult
Dim db As New Model1Entities
Dim m As Node =(从n在db.Node _
其中n.Id = id _
选择n).First
返回视图(m)
结束函数

结束类

我已经清理了我的视图。

 <%@ Import Namespace =ClassLibrary1%> 
<%@ Page Title =Language =VBMasterPageFile =〜/ Views / Shared / Site.MasterInherits =System.Web.Mvc.ViewPage(Of Node)%>

我不再看到警告。



但是我仍然得到运行时错误。

解决方案

仔细检查您的Views / Web.config是否具有引用的命名空间项目:

 < 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,System.Web.Mvc,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31BF3856AD364E35
userControlBaseType =System.Web.Mvc.ViewUserControl,System.Web .Mvc,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31BF3856AD364E35>
< controls>
< add assembly =System.Web.Mvc,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31BF3856AD364E35namespace =System.Web.MvctagPrefix =mvc/>
< / controls>
< namespaces>
< add namespace =SolutionName.WpfApplication1.Model1Entities/>
< / namespaces>
< / pages>

< /system.web>

< system.webServer>
< validation validateIntegratedModeConfiguration =false/>
<处理程序>
< remove name =BlockViewHandler/>
< add name =BlockViewHandlerpath =*verb =*preCondition =integratedModetype =System.Web.HttpNotFoundHandler/>
< / handlers>
< /system.webServer>
< / configuration>

更新3:
运行时错误看起来不是MVC项目。 >

I've got an ADO.NET Entity Framework class called Node in a WPF project. I want to use it in a different ASP.NET MVC project within the same solution.

My Node controller:

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

When I run the project and try to navigate to http://.../Node/Display/[a valid ID]

I get an error on my Display action:

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

I've read that error can be due to a codebehind class naming conflict. But I don't think that's the case here.

Can I not use a Model from a different project to create a strongly-typed ASP.NET MVC View?

Update 1:

I've tried importing the namespace on my Display.aspx page, but none of

<%@ Import Namespace="WpfApplication1" %>

or

<%@ Import Namespace="SolutionName.WpfApplication1" %>

or

<%@ Import Namespace="SolutionName.WpfApplication1.Model1Entities" %>

prevent the error.

Update 2:

I've tried adding the namespace to my Views/Web.config file, but niether

<add namespace="SolutionName.WpfApplication1" />

nor

<add namespace="SolutionName.WpfApplication1.Model1Entities" />

prevent the error.

Update 3:

Since adding the namespace, I do now get this Warning:

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.

Is that a clue?

Update 4:

I've moved the model from my WpfApplication1 to a new ClassLibrary1.

I've cleaned up my Controller.

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

I've cleaned up my View.

<%@ Import Namespace="ClassLibrary1" %>
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of Node)" %>

I no longer see the Warning.

But I still get the runtime error.

解决方案

Double-check that your Views/Web.config has the namespace of your referenced project:

<system.web>
  <pages>
    <namespaces>
      <add namespace="SolutionName.WpfApplication1.Model1Entities" />
    </namespaces>
  <pages>
</system.web>

UPDATE:

I don't have any WPF applications to test this on...I thought you were attempting to use a "Class Library" project. That will definitely work. Is it possible to refactor your application slightly to pull out the "Models" into their own project? That way you can compile it as a Class Library.

UPDATE 2:

Odd, it's almost as if your page is not inheriting correctly from System.Web.Mvc. Make sure your Views/Web.config looks like this:

<?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, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
            userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=1.0.0.0, Culture=neutral, 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>

UPDATE 3: The runtime error looks like it's not an MVC project.

这篇关于ASP.NET MVC视图可以使用来自不同项目的模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆