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

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

问题描述

我有所谓的节点在WPF项目中的ADO.NET实体框架类。我想在同一解决方案中使用它在不同的ASP.NET 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.

我的节点控制器:


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]

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.

编译错误

编译器错误信息:BC30456:
  标题是不是会员
  ASP.views_node_display_aspx。

Compiler Error Message: BC30456: 'Title' is not a member of 'ASP.views_node_display_aspx'.

源错误:

1号线:其中;%@页标题=LANGUAGE =VB的MasterPageFile =〜/查看/共享/的Site.Master继承=System.Web.Mvc.ViewPage(中WpfApplication1.Node) %>

Line 1: <%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of WpfApplication1.Node)" %>

源文件:
  C:\\ MvcApplication1 \\查看\\节点\\ Display.aspx

Source File: C:...\MvcApplication1\Views\Node\Display.aspx

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

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

我不能用模型从不同的项目,以创建一个强类型的ASP.NET MVC视图?

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

我试过导入我的Display.aspx页面上的命名空间,但没有

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

<%@ Import Namespace="WpfApplication1" %>

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

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

prevent错误。

prevent the error.

我已经尝试添加命名空间我的意见/ Web.config文件,但niether

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

<add namespace="SolutionName.WpfApplication1" />

也没有

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

prevent错误。

prevent the error.

由于添加的命名空间,我现在得到这样的警告:

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

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

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.

那是一个线索?

我搬到模型从我WpfApplication1到一个新的ClassLibrary1的。

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.

推荐答案

仔细检查你的浏览/ Web.config中有你引用的项目的命名空间:

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>

更新:

我没有任何WPF应用程序在测试这个......我还以为你试图使用一个类库项目。这肯定会工作。是否有可能重构应用程序稍微拉出模式到自己的项目?这样,你可以编译成类库。

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.

更新2:

奇怪,这几乎就像如果你的页面不是从System.Web.Mvc正确继承。确保您查看/ Web.config中是这样的:

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>

更新3:
运行时错误看起来像它不是一个MVC项目。

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

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

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