griegs
griegs

Reputation: 22760

MVC, Views and System.Data.Entity

I have a Repository Project that is added to my MVC application.

Within my C# code, of the MVC Web App, I have had to add a reference to System.Data.Entity so that I can access the objects from my Repository.

So the following fails if I do not have the reference added;

DataRepository<Store> repo = new DataRepository<Store>();
List<Store> allSorted = repo.All(x => x.name);

Now I want to pass that list to my Partial View, which sits within a FVM and it's the FVM that I pass to the Partial View.

So the index.aspx code;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<StoresFVM>" %>

<% Html.RenderPartial("StoreList", Model.StoreListFVM); %>

And the ASCX code;

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<StoreListFVM>" %>

<%        
    SelectList storeList = new SelectList(Model.Stores.Select(x => new { Value = x.number, Text = x.name }),"Value","Text");
%>

<%= Html.DropDownList("SelectedStore", storeList) %>

However, I get an error informing me that I need to include a reference to System.Data.Entity in the ascx. I don't get why for one.

I have tried adding the namespace to the web.config file and I have tried importing the namespace at the top of the ascx page.

Thoughts?

EDIT

\nasfile02\Visual Studio 2010\Projects\TOM\TOM\Views\Stores\PartialViews\StoreList.ascx(4): error CS0012: The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

EDIT

namespace TOM.FormViewModels
{
    public class StoresFVM
    {
        public StoreListFVM StoreListFVM { get; set; }
    }
}

namespace TOM.FormViewModels
{
    public class StoreListFVM
    {
        public List<Store> Stores { get; set; }
    }
}

Upvotes: 2

Views: 7178

Answers (1)

JodyT
JodyT

Reputation: 4412

You should make sure the web.config looks as following:

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </assemblies>
</compilation>

Also described at: https://stackoverflow.com/a/3611040/1737862

Upvotes: 17

Related Questions