MVC多对多查看 [英] MVC Many to Many View

查看:102
本文介绍了MVC多对多查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次我有问题,我的项目。
我曾与EF 5的DbContext生成器创建的两个模型:

Again I have problem with my project. I have two models created with EF 5 DBContext Generator:

首先:

    public int ID_AN { get; set; }
    public string TITLE_OR { get; set; }
    public string TITLE_EN { get; set; }

    public virtual ICollection<GENRES> GENRES { get; set; }

二:

    public int ID_GE { get; set; }
    public string GENRE { get; set; }

    public virtual ICollection<ANIME> ANIME { get; set; }

在我创建控制器:

    public ActionResult Details(int id)
    {
        using (var db = new MainDatabaseEntities())
        {
            return View(db.ANIME.Find(id););             
        }
    }

和查看:

@model AnimeWeb.Models.ANIME

@{
ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
<legend>ANIME</legend>

<div class="display-label">
     @Html.DisplayNameFor(model => model.TITLE_OR)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.TITLE_OR)
</div>

<div class="display-label">
     @Html.DisplayNameFor(model => model.TITLE_EN)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.TITLE_EN)
</div>

</fieldset>

要至此一切工作正常,但我想,以显示所选的动画所有类型。当我尝试添加

To this point everything works fine, but I would like to display all Genres of selected anime. When I try to add

<div>
    @Html.DisplayFor(model => model.GENRES)
</div>

我得到一个错误:ObjectContext的实例已经被处置,不能再用于需要连接的业务。

I get an error: "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."

我是新来的MVC,所以我会很gratefull如果有人能向我解释如何使人们有可能工作。

I'm new to MVC so I would be very gratefull if someone could explain to me how to make it possible to work.

推荐答案

您可以使用包括扩展方法来加载流派,像这样的:

you can use "include" extended method to load GENRES,like this:

db.ANIME.Include("GENRES").Find(id)

如果你想使用 @ Html.DisplayFor(型号=&GT; model.GENRES)来显示所有的generes的,你可以用DisplayTemplates做到这一点,看看这个<一href=\"http://www.growingwiththeweb.com/2012/12/aspnet-mvc-display-and-editor-templates.html#.UoQ9RvlTSnd\"相对=nofollow> ASP.NET MVC的显示​​和编辑模板。

if you want to use @Html.DisplayFor(model => model.GENRES) to show all of the generes, you can do it with DisplayTemplates,check this ASP.NET MVC display and editor templates.

这篇关于MVC多对多查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆