I am new in entity framework and ASP.NET. i am trying to access attributes of navigation properties. but i am facing the "The ObjectContext instance has been disposed" error. than i have do some google and found that it can be fixed by the data = db.tblCourses.Include(c => c.tblSection).ToList();
but i am still facing the problem.
Model:
using System.Data.Entity;
public List<tblCourse> editedCoursesView()
{
List<tblCourse> data;
using (enrolmentsystemEntities db = new enrolmentsystemEntities())
{
data = db.tblCourses.ToList();
data = db.tblCourses.Include(c => c.tblSection).ToList();
return data;
}
}
tblCourse:
namespace enrolmentSystem.Models
{
using System;
using System.Collections.Generic;
public partial class tblCourse
{
public tblCourse()
{
this.tblenrolments = new HashSet<tblenrolment>();
}
public int CourseId { get; set; }
public string CourseName { get; set; }
public int departmentID { get; set; }
public int TeacherId { get; set; }
public int CreditHours { get; set; }
public int SectionId { get; set; }
public virtual tbldepartment tbldepartment { get; set; }
public virtual tblteacher tblteacher { get; set; }
public virtual ICollection<tblenrolment> tblenrolments { get; set; }
public virtual tblSection tblSection { get; set; }
}
}
tblRoom :
namespace enrolmentSystem.Models
{
using System;
using System.Collections.Generic;
public partial class tblRoom
{
public tblRoom()
{
this.tblSections = new HashSet<tblSection>();
}
public int RoomNo { get; set; }
public string Block { get; set; }
public int Capacity { get; set; }
public virtual ICollection<tblSection> tblSections { get; set; }
}
}
Controller:
[HttpGet]
public ActionResult editCourse()
{
course c = new course();
List<tblCourse> data = c.editedCoursesView();
return View("editCourse", data);
}
View:
@model List<enrolmentSystem.Models.tblCourse>
@{
ViewBag.Title = "editCourse";
}
<h2>Edit Course</h2>
<table border="1">
<tr style="text-align:center;">
<td>Course ID</td>
<td>Course Name</td>
<td> Course Department ID</td>
<td> Course Teacher ID</td>
<td> Course CreditHours</td>
<td> Course StartTime</td>
<td> Course EndTime</td>
<td> MaxCapacity </td>
<td> Course RoomNo </td>
</tr>
@if (Model != null)
{
foreach (var i in Model)
{
<form action="~/Enrolment/editCourse" method="post">
<tr>
<td>@i.CourseId <input name="id" type="hidden" value="@i.CourseId" /> </td>
<td><input type="text" name="name" value="@i.CourseName" required /></td>
<td><input type="number" name="dpId" value="@i.departmentID" required /></td>
<td><input type="number" name="teacherId" value="@i.TeacherId" required /></td>
<td><input type="number" name="creditHours" value="@i.CreditHours" required /></td>
<td><input type="text" name="startTime" value="@i.tblSection.startTime" required /></td>
<td><input type="text" name="endTime" value="@i.tblSection.endTime" required /></td>
<td><input type="number" name="capacity" value="@i.tblSection.tblRoom.Capacity" required /></td>
<td><input type="number" name="roomNo" value="@i.tblSection.RoomNo" required /></td>
<td> <input type="submit" value="Update Course" /> </td>
</tr>
</form>
}
}
</table>