Mark Tabor

Mark Tabor

  • 572
  • 1.9k
  • 431k

Cannot convert list to Ienumerable in mvc

Feb 15 2020 12:50 PM
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
  1. public class StudentCoursesViewModel  
  2. {  
  3. public Moduel Modules { getset; }  
  4. public Years Year { getset; }  
  5. public Student studnets { getset; }  
  6. public Programs programs { getset; }  
  7. public IEnumerable<Courses> curses { getset; }  
  8. public Courses courses { getset; }  
  9. public Student_Assigned_courses studentAssignedCourses { getset; }  
  10. }  
  11. }  
My controller :
  1. using (DatabaseContext db = new DatabaseContext())  
  2. {  
  3. List<Student_Assigned_courses> Student_Assigned_courses = db.StudentCoursesAssigned.ToList();  
  4. List<Courses> courses = db.Courses.ToList();  
  5. List<Moduel> Module = db.Moduels.ToList();  
  6. List<Years> year = db.Years.ToList();  
  7. List<Programs> Programs = db.Programs.ToList();  
  8. List<Student> students = db.Students.ToList();  
  9. List<Courses> curr = db.Courses.ToList();  
  10. var employeeRecord = from e in Student_Assigned_courses  
  11. join d in students on e.Student_id equals d.Student_Id into table1  
  12. from d in table1.ToList()  
  13. join h in courses on e.Course_Id equals h.Course_Id into table6  
  14. from h in table6.ToList()  
  15. join i in courses on e.Course_Id equals i.Course_Id into table2  
  16. from i in table2.ToList()  
  17. join m in Module on e.Module_Id equals m.Id into table3  
  18. from m in table3.ToList()  
  19. join y in year on e.Year_Id equals y.Id into table4  
  20. from y in table4.ToList()  
  21. join p in Programs on e.Program_Id equals p.Id into table5  
  22. from p in table5.ToList()  
  23. select new StudentCoursesViewModel  
  24. {  
  25. studentAssignedCourses = e,  
  26. courses = i,  
  27. programs = p,  
  28. curses=h,  
  29. studnets = d,  
  30. Year = y,  
  31. Modules = m,  
  32. };  
  33. return View(employeeRecord);  
  34. }  
My view:
  1. @model IEnumerable<CMS_Monitoring.ViewModels.StudentCoursesViewModel>  
  2. @{  
  3. ViewBag.Title = "Index";  
  4. Layout = "~/Views/Shared/_Layout2.cshtml";  
  5. }  
  6. <h2>Index</h2>  
  7. <p>  
  8. @Html.ActionLink("Create New""Create")  
  9. </p>  
  10. <table class="table">  
  11. <tr>  
  12. <th>  
  13. @Html.DisplayNameFor(model => model.studnets.Student_FName)  
  14. </th>  
  15. <th>  
  16. @Html.DisplayNameFor(model => model.courses.Course_Name)  
  17. </th>  
  18. <th>  
  19. @Html.DisplayNameFor(model => model.courses.Status)  
  20. </th>  
  21. <th>  
  22. @Html.DisplayNameFor(model => model.studentAssignedCourses.Date_Created)  
  23. </th>  
  24. @*<th>  
  25. @Html.DisplayNameFor(model => model.studentAssignedCourses.Date_Modified)  
  26. </th>*@  
  27. <th>  
  28. @Html.DisplayNameFor(model => model.studentAssignedCourses.Assigned_By)  
  29. </th>  
  30. <th>  
  31. @Html.DisplayNameFor(model => model.Modules.Name)  
  32. </th>  
  33. <th>  
  34. @Html.DisplayNameFor(model => model.programs.Program_Title)  
  35. </th>  
  36. <th>  
  37. @Html.DisplayNameFor(model => model.Year.Name)  
  38. </th>  
  39. <th>Actions</th>  
  40. </tr>  
  41. @foreach (var item in Model) {  
  42. <tr>  
  43. <td>  
  44. @Html.DisplayFor(modelItem => item.studnets.Student_FName)  
  45. </td>  
  46. <td>  
  47. @Html.DisplayFor(modelItem => item.courses.Course_Name)  
  48. </td>  
  49. <td>  
  50. @{  
  51. foreach (var course in item.curses)  
  52. {  
  53. @course.Course_Code @: @course.Course_Name <br />  
  54. }  
  55. }  
  56. </td>  
  57. <td>  
  58. @Html.DisplayFor(modelItem => item.courses.Status)  
  59. </td>  
  60. <td>  
  61. @Html.DisplayFor(modelItem => item.studentAssignedCourses.Date_Created)  
  62. </td>  
  63. @*<td>  
  64. @Html.DisplayFor(modelItem => item.studentAssignedCourses.Date_Modified)  
  65. </td>*@  
  66. <td>  
  67. @Html.DisplayFor(modelItem => item.studentAssignedCourses.Assigned_By)  
  68. </td>  
  69. <td>  
  70. @Html.DisplayFor(modelItem => item.Modules.Name)  
  71. </td>  
  72. <td>  
  73. @Html.DisplayFor(modelItem => item.programs.Program_Title)  
  74. </td>  
  75. <td>  
  76. @Html.DisplayFor(modelItem => item.Year.Name)  
  77. </td>  
  78. <td>  
  79. @Html.ActionLink("Edit""Edit"new { id = item.studentAssignedCourses.Id }) |  
  80. @Html.ActionLink("Details""Details"new { id = item.studentAssignedCourses.Id }) |  
  81. @Html.ActionLink("Delete""Delete"new { id = item.studentAssignedCourses.Id })  
  82. </td>  
  83. </tr>  
  84. }  
  85. </table>  
Below error screen
 

Answers (4)