Can ASP.NET MVC View use a model from another project?

I have an ADO.NET Entity Framework class called Node in a WPF project. I want to use it in another ASP.NET MVC project as part of 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 switch to http://.../Node/Display/[a valid ID]

I get an error in the Display action:

Server error in application "/".

Compilation error

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

Source Error:

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

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

I read that the error could be caused by a naming conflict to the codebehind class . But I don’t think the point is here.

Can I use a model from another project to create a strongly typed ASP.NET MVC view?

Update 1:

I tried to import 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 error.

Update 2:

I tried adding a namespace to my Views / Web.config file, but niether

 <add namespace="SolutionName.WpfApplication1" /> 

neither

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

prevent error.

Update 3:

Since adding the namespace, now I get this warning:

The namespace or type specified in the Import Solution Name.WpfApplication1 'does not contain any public members or Cannot be found. Ensure that the namespace or type is defined and contains at least one public member. Make sure that the name of the imported item does not use any aliases.

Is this a clue?

Update 4:

I moved the model from my WpfApplication1 to the new ClassLibrary1.

I cleaned 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 cleared 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 a runtime error.

+7
asp.net-mvc entity-framework
source share
3 answers

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

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

UPDATE:

I do not have WPF applications to test this ... I thought you were trying to use the Class Library project. It will definitely work. Is it possible to reorganize your application a bit to pull the “Models” into your own project? This way you can compile it as a class library.

UPDATE 2:

Odd, it's almost as if your page did not inherit correctly from System.Web.Mvc. Make sure your /Web.config views look 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: A runtime error looks like this is not an MVC project.

+8
source share

You definitely can! Just make sure you include the correct namespaces in your view:

 <%@ Import Namespace="MyEntityNamespace" %> 
+6
source share

Right-click the links in the web project and select "add link." After that use the "project".

After that, you can go with the answer provided by Rex (no shure about Vb syntax there. I use C #, and this is C # syntax)

This works for me.

EDIT:

using

not

+1
source share

All Articles