Is it possible to utilize a Model from another project in an ASP.NET MVC View?

asp.net-mvc entity-framework

Question

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.

1
7
7/17/2012 10:21:41 PM

Popular Answer

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.

8
3/16/2009 2:20:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow