Hi i have mvc web application in which i am stroing courses and students into the table like below
Now in my database table i am saving separate rows for each course & students like student A is having 2 courses and student B is having 1 course as shown above.
Right now My index view is shows three rows what i need to show 2 rows like i need 1 row for each students having comma separated courses below are my model class
- public class StudentCoursesViewModel
- {
- public Moduel Modules { get; set; }
- public Years Year { get; set; }
- public Student studnets { get; set; }
- public Programs programs { get; set; }
- public IEnumerable<Courses> curses { get; set; }
- public Courses courses { get; set; }
- public Student_Assigned_courses studentAssignedCourses { get; set; }
- }
- }
My controller :
- using (DatabaseContext db = new DatabaseContext())
- {
- List<Student_Assigned_courses> Student_Assigned_courses = db.StudentCoursesAssigned.ToList();
- List<Courses> courses = db.Courses.ToList();
- List<Moduel> Module = db.Moduels.ToList();
- List<Years> year = db.Years.ToList();
- List<Programs> Programs = db.Programs.ToList();
- List<Student> students = db.Students.ToList();
- List<Courses> curr = db.Courses.ToList();
- var employeeRecord = from e in Student_Assigned_courses
- join d in students on e.Student_id equals d.Student_Id into table1
- from d in table1.ToList()
- join h in courses on e.Course_Id equals h.Course_Id into table6
- from h in table6.ToList()
- join i in courses on e.Course_Id equals i.Course_Id into table2
- from i in table2.ToList()
- join m in Module on e.Module_Id equals m.Id into table3
- from m in table3.ToList()
- join y in year on e.Year_Id equals y.Id into table4
- from y in table4.ToList()
- join p in Programs on e.Program_Id equals p.Id into table5
- from p in table5.ToList()
- select new StudentCoursesViewModel
- {
- studentAssignedCourses = e,
- courses = i,
- programs = p,
- curses=h,
- studnets = d,
- Year = y,
- Modules = m,
- };
- return View(employeeRecord);
- }
My view:
- @model IEnumerable<CMS_Monitoring.ViewModels.StudentCoursesViewModel>
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout2.cshtml";
- }
- <h2>Index</h2>
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- <table class="table">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.studnets.Student_FName)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.courses.Course_Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.courses.Status)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.studentAssignedCourses.Date_Created)
- </th>
- @*<th>
- @Html.DisplayNameFor(model => model.studentAssignedCourses.Date_Modified)
- </th>*@
- <th>
- @Html.DisplayNameFor(model => model.studentAssignedCourses.Assigned_By)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Modules.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.programs.Program_Title)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Year.Name)
- </th>
- <th>Actions</th>
- </tr>
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.studnets.Student_FName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.courses.Course_Name)
- </td>
- <td>
- @{
- foreach (var course in item.curses)
- {
- @course.Course_Code @: @course.Course_Name <br />
- }
- }
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.courses.Status)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.studentAssignedCourses.Date_Created)
- </td>
- @*<td>
- @Html.DisplayFor(modelItem => item.studentAssignedCourses.Date_Modified)
- </td>*@
- <td>
- @Html.DisplayFor(modelItem => item.studentAssignedCourses.Assigned_By)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Modules.Name)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.programs.Program_Title)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Year.Name)
- </td>
- <td>
- @Html.ActionLink("Edit", "Edit", new { id = item.studentAssignedCourses.Id }) |
- @Html.ActionLink("Details", "Details", new { id = item.studentAssignedCourses.Id }) |
- @Html.ActionLink("Delete", "Delete", new { id = item.studentAssignedCourses.Id })
- </td>
- </tr>
- }
- </table>
Below error screen