MVC Many to Many View

邮差的信 提交于 2020-01-17 10:00:22

问题


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

First:

    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; }

Second:

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

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

After that I created controller:

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

And View:

@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>

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

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


回答1:


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

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

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.




回答2:


This is an Entity Framework issue actually. Once you leave your Controller you are too late to be querying the database. Entity Framework is lazy loading (aka on demand) the Genres. To fix this you should change your query to explicitly load the Genres when retrieving the Anime object.

Update: Never told you how to do that. There are many ways to explicitly or eager load your Genres. The best resource for that would be to read the MSDN page on Loading Related Entities. One way would be to add code similiar to the following in your controller (I think this works with how you have it setup).

public ActionResult Details(int id)
{
    using (var db = new MainDatabaseEntities())
    {
        var anime = db.ANIME.Find(id);
        db.Entry(anime).Collection(a => a.GENRES).Load();
        return View(anime);             
    }
}



回答3:


Consider initializing the context when the controller is initialized, and disposing of it in the controller's dispose method.

This is the pattern (incidentally) that is used by the templates in visual studio.

public class DemoController : Controller
{
    private MainDatabaseEntities db = new MainDatabaseEntities();

    public ActionResult Details(int id)
    {
        return View(db.ANIME.Find(id));
    }

    protected override void Dispose(bool disposing)
    {
        this.db.Dispose();
        base.Dispose(disposing);
    }
}


来源:https://stackoverflow.com/questions/19966159/mvc-many-to-many-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!