Hi i have mvc web application in which i am stroing courses and students into the table like below
| ID | student_Id | course_Id |
| 1 | 1 | 2 |
| 2 | 1 | 1 |
| 3 | 2 | 1 |
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
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 = db.StudentCoursesAssigned.ToList(); - List
courses = db.Courses.ToList(); - List
Module = db.Moduels.ToList(); - List
year = db.Years.ToList(); - List
Programs = db.Programs.ToList(); - List
students = db.Students.ToList(); - List
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
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout2.cshtml";
- }
Index
- @Html.ActionLink("Create New", "Create")
class="table">
- @Html.DisplayNameFor(model => model.studnets.Student_FName)
- @Html.DisplayNameFor(model => model.courses.Course_Name)
- @Html.DisplayNameFor(model => model.courses.Status)
- @Html.DisplayNameFor(model => model.studentAssignedCourses.Date_Created)
- @*
*@- @Html.DisplayNameFor(model => model.studentAssignedCourses.Date_Modified)
- @Html.DisplayNameFor(model => model.studentAssignedCourses.Assigned_By)
- @Html.DisplayNameFor(model => model.Modules.Name)
- @Html.DisplayNameFor(model => model.programs.Program_Title)
- @Html.DisplayNameFor(model => model.Year.Name)
Actions - @foreach (var item in Model) {
- @Html.DisplayFor(modelItem => item.studnets.Student_FName)
- @Html.DisplayFor(modelItem => item.courses.Course_Name)
- @{
- foreach (var course in item.curses)
- {
- @course.Course_Code @: @course.Course_Name
- }
- }
- @Html.DisplayFor(modelItem => item.courses.Status)
- @Html.DisplayFor(modelItem => item.studentAssignedCourses.Date_Created)
- @*
*@- @Html.DisplayFor(modelItem => item.studentAssignedCourses.Date_Modified)
- @Html.DisplayFor(modelItem => item.studentAssignedCourses.Assigned_By)
- @Html.DisplayFor(modelItem => item.Modules.Name)
- @Html.DisplayFor(modelItem => item.programs.Program_Title)
- @Html.DisplayFor(modelItem => item.Year.Name)
- @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 })
- }
Below error screen

Mark TaborPosted Feb 15, 2020, 11:38 PM
Mark TaborPosted Feb 15, 2020, 11:37 PM
Mark TaborPosted Feb 15, 2020, 11:35 PM
Joginder BangerPosted Feb 15, 2020, 9:54 PM