views:

1502

answers:

3

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.

+6  A: 

You definitely can! Just make sure you include the right namespaces in your View:

<%@ Import Namespace="MyEntityNamespace" %>
Rex M
It's not working. See update in question.
Zack Peterson
Importing the namespace generates a warning. See update in question.
Zack Peterson
+7  A: 

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.

Peter J
It's not working. See update in question.
Zack Peterson
I'm sure this goes without saying, but did you check for typos? Do you have any namespace pollution (same classnames across different namespaces)? Your update says (Of wpfapplication1.Node), but the proper method (if namespace references are correct) would just be (Of Node).
Peter J
Importing the namespace generates a warning. See update in question.
Zack Peterson
Namespaces can be different than Solution Names and Project Names.
Peter J
You're right. With the Import line in my View, I can shorten my Inherits to "...(Of Node)". And I've added an Imports line to my controller and shortened my db and m declarations. But I've still got the warning and run-time error.
Zack Peterson
My namespace and assembly name match.
Zack Peterson
Strange...I generally get that error when attempting to do an imports/using on a namespace which I have not added a reference to.
Peter J
+1  A: 

Right click references in the web-project and select "add reference". After that use "project".

After that you can go with the answer provided by Rex(not shure about the Vb syntax there. I use c# and it is the syntax of c# )

It works for me.

EDIT:

use

<%@ Import Namespace="WpfApplication1" %>

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

Malcolm Frexner
I already have a reference to the WpfApplication1 project.
Zack Peterson
Sorry didnt check your controller
Malcolm Frexner
That <%@ Import ... %> didn't fix it either.
Zack Peterson