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?
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.
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.
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?
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.
That's it! Although you'll need to do something with your models, either put them together with views or have a third project for them - otherwise you'll have a circular dependency. Another drawback is that everyone who will be working with the views will need that Razor Generator extension.
You can use multiple models in a single view by creating a common model for all the models that are to be used in a single view. To achieve this, refer to the following steps. First, create a new model (common for all models) and refer all other models that are to be used in the same view.
You can't. You need to represent different contexts as different view models because validation on those models changes depending on the screen representation.
The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.
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.
You definitely can! Just make sure you include the right namespaces in your View:
<%@ Import Namespace="MyEntityNamespace" %>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With