开发者

MVC 3 Razor View, use Entitiy Model in another project

I'm in the process of learning MVC 3, Razor, and EF Model First.

I have a project I'm working on where I have defined the EF Model in a separate project from the main web project. I'm trying to access data using that model in a view.

I get this error:

MVC 3 Razor View, use Entitiy Model in another project

I added the System.Data.Entity to my references.

Controller:

public ActionResult ListRole()
{
    AuthDbContainer db = new AuthDbContainer();
    List<Role> roles = db.Roles.ToList();

    return View(roles);
}

View:

@model IEnumerable<WebSecurity.Role>
@{
    ViewBag.Title = "Role List";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
    <title>ListRole</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table>
        <tr>
            <th>
                desc
            </th>
            <th>
                createDate
            </th>
            <th>
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.desc)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.createDate)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.roleName }) |
                    @Html.ActionLink("Details", "Details", new { id = item.roleName }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.roleName })
                </td>
            </tr>
        }
    </table>
</body>
</html>

Thank you for any help.

UPDATE

This error happened because the assembly reference in my web.config wa开发者_如何转开发s missing. It was added to my references in the project but not in the web.config. IIS pretty much tells me that in my error message. I should have read it better. Sorry for wasting anyones time. I added the following to my web.config and it works great now:

<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />


Adding namespace to the Web.Config is not enough. When you add them to the Web.Config they serve as using directives in your views.

So you need Reference System.Data.Entity in your MVC project. (Right click References in your MVC project and click Add Reference and so forth ...)

Furthermore if you want to add using directives to your Razor views, you need to add it as follows

You need to configure section groups as follows

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

Then

  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
        <add namespace="System.Data.Entity" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Did you add a reference to EntityFramework in your MVC project?


This error happened because the assembly reference in my web.config was missing. It was added to my references in the project but not in the web.config. IIS pretty much tells me that in my error message. I should have read it better. Sorry for wasting anyones time. I added the following to my web.config and it works great now:

<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜