Asp.net mvc 在mvc 3表单上显示子项?

Asp.net mvc 在mvc 3表单上显示子项?,asp.net-mvc,entity-framework,list,Asp.net Mvc,Entity Framework,List,(警告新手) 我正在使用codefirst实体框架(与MVC3一起),并试图显示步骤列表及其相关问题。不确定我的语法为何引发异常“ObjectContext实例已被释放,无法再用于需要连接的操作。”: 当item.Questions为access时,EF将尝试点击数据库以获取该项目的问题。如果上下文已被释放,它将失败。由于您将在视图中循环处理这些问题,因此我建议使用“Include”将它们添加到初始查询中 @model IEnumerable<COPSGMIS.Models.Step>

(警告新手) 我正在使用codefirst实体框架(与MVC3一起),并试图显示步骤列表及其相关问题。不确定我的语法为何引发异常“ObjectContext实例已被释放,无法再用于需要连接的操作。”:


当item.Questions为access时,EF将尝试点击数据库以获取该项目的问题。如果上下文已被释放,它将失败。由于您将在视图中循环处理这些问题,因此我建议使用“Include”将它们添加到初始查询中

@model IEnumerable<COPSGMIS.Models.Step>

@{
    ViewBag.Title = "Questionaire";
}

<h2>Questionaire</h2>
   @using (Html.BeginForm("Questionaire", "Question", FormMethod.Post, new { id = "SignupForm" }))
   {
       <div>
            @foreach (var item in Model)
            {
              <fieldset class="wizard">
                <legend class="wizard">@Html.DisplayFor(modelItem => item.Title)</legend>
                @foreach (var question in item.Questions)
                //*** item.Questions is throwing an exception  ****
                {
                <label for="question">@Html.DisplayFor(modelItem => question.QuestionText)</label>
                <input id="question" type="text" /> 
                }
              </fieldset>
            }            
        <p>
            <input id="SaveAccount" type="button" value="Save" />
        </p>
    </div>
   }
public int StepID { get; set; }
        public int ReviewID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int StepOrder { get; set; }
        public virtual ICollection<Question> Questions { get; set; }
var steps = from b in db.Steps
                            orderby b.StepOrder
                            select b;
                return View(steps.ToList());
var steps = from b in db.Steps.Include(s => s.Questions)
            orderby b.StepOrder
            select b;

return View(steps.ToList());