8

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.

3
  • What was the final resolution? Commented Mar 21, 2009 at 4:24
  • Just to get it working, I've duplicated the ADO.NET Entity Framework model from the WPF project into the ASP.NET MVC project. That's an ugly fix. I still need to go back and figure this out properly. I'm pretty sure it's just a naming conflict of some sort, but I've not confirmed that yet. Commented Mar 21, 2009 at 12:00
  • Next week I'm going to have to add a few columns to my database tables. I don't want to have to update my Entity model in two different places. So, I'll try to take that as an opportunity to get that third, ClassLibrary type project to work. Commented Mar 21, 2009 at 12:03

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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).
Importing the namespace generates a warning. See update in question.
Namespaces can be different than Solution Names and Project Names.
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.
My namespace and assembly name match.
|
6

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

<%@ Import Namespace="MyEntityNamespace" %>

1 Comment

Importing the namespace generates a warning. See update in question.
1

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

not

2 Comments

Sorry didnt check your controller
That <%@ Import ... %> didn't fix it either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.