简体   繁体   English

MVC4模型在查看问题中:在调试模式下模型缺少详细信息

[英]MVC4 Model in View issue: in debug mode Model is missing details

I encountered a very strange (and annoying) issue: in MVC 4 web application while loading data from the database and using a foreach on the Model within the view: 我遇到了一个非常奇怪(令人讨厌)的问题:在MVC 4 Web应用程序中,从数据库加载数据并在视图内的模型上使用foreach时:

@foreach (var meeting in Model)

i have a breakpoint in the method's beginning and examining the object meeting i see that it misses some data (reference to other tables). 我在方法的开头有一个断点,并检查对象会议,我发现它缺少一些数据(对其他表的引用)。 if I open (+) other object within meeting the missing data appears. 如果我在会议中打开(+)其他对象,则会出现丢失的数据。

Why? 为什么?

Thanks! 谢谢!

here is my controller method: 这是我的控制器方法:

        public ActionResult GetMeetingMR(int id = 0)
    {
        var meetingPurpose = db.MeetingPurposes.ToList();
        ViewBag.MeetingPurpose = new SelectList(meetingPurpose, "MeetingPurposeID", "MeetingPurposeName");
        ViewBag.MRID = id;
        List<Meeting> meetings = (db.MortgageRequests.Find(id)).Meetings.ToList();
        return View(meetings);

    }

Including your title in your question, my guess is you do not experience the problem (yet) without the debugger attached. 在您的问题中加上您的标题后,我想您可能会在没有附加调试器的情况下遇到问题。
As others have commented it is due to LazyLoading . 正如其他人所评论的,这是由于LazyLoading所致。
The problem may occur sometimes because while rendering the View the entities container is being disposed, most probably because it is collected by the GarbageCollector which calls the destructor of the container. 有时可能会出现此问题,因为在渲染“ View ”实体容器时,很可能是因为它是由GarbageCollector收集的,该容器调用了容器的析构函数。

If you wish to access the entities on your View you can use Include on the ObjectSet . 如果希望访问View上的实体,则可以在ObjectSet上使用Include Let's say MeetingPurposes has an associated EntitySet of Dates : If you wish to include them in the loadoperation you can do the following: db.MeetingPurposes.Include("Dates").ToList(); 比方说, MeetingPurposes有一个相关EntitySetDates :如果您想将其包含在你可以做以下的loadoperation: db.MeetingPurposes.Include("Dates").ToList();

public ActionResult GetMeetingMR(int id = 0)
{
    //Load the MeetingPurposes including their dates
    var meetingPurpose = db.MeetingPurposes.Include("Dates").ToList();
    ViewBag.MeetingPurpose = new SelectList(meetingPurpose,
                                            "MeetingPurposeID",
                                            "MeetingPurposeName");
    ViewBag.MRID = id;
    List<Meeting> meetings = (db.MortgageRequests.Find(id)).Meetings.ToList();
    return View(meetings);
}

If you wish to do this for Meetings and Meetings has an EntitySet of Dates you could do: 如果您希望为Meetings执行此操作,并且会议具有DatesEntitySet ,则可以执行以下操作:

public ActionResult GetMeetingMR(int id = 0)
{
    var meetingPurpose = db.MeetingPurposes.ToList();
    ViewBag.MeetingPurpose = new SelectList(meetingPurpose,
                                            "MeetingPurposeID",
                                            "MeetingPurposeName");
    ViewBag.MRID = id;
    var meetings = (from mee in db.Meetings.Include("Dates")
                    join mga in dbo.MortgageRequests.Where(m => m.Id == id)
                    on mee.MortgageRequestID equals mga.ID
                    select mee).ToList();
    return View(meetings);
}

Not really sure what your Find extension method is so I used a Where on MortgageRequests . 不太确定您的Find扩展方法是什么,因此我在MortgageRequests上使用了Where

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM