Mark Tabor

Mark Tabor

  • 587
  • 2k
  • 460.6k

Making a single row with comma separated list in mvc

Jan 5 2020 5:34 AM
Hi I have a mvc application in which I have teacher, courses and techervs courses table , Now i am assigning multiple courses to a signle teacher as show below in my table 
 
Teacher Table:
 id     name
 1  GGG
 2  CCCC
 
Courses Table
 id  name
 1  Math
 2  geography
 
TeacherVsCourses:
 Id  course_id  Teacher_Id
 1  1  1
 2  2  1
 3  3  2
 4  1  2
 
Now below is my contorller code:
  1. public ActionResult Index(string sortOrder, string currentFilter, string SearchString, int? page)  
  2. {  
  3. ViewBag.Course_Id = _ITeacherCoruses.PopulateCourses();  
  4. ViewBag.Teacher_Id = _ITeacherCoruses.PopulateTeachers();  
  5. ViewBag.Semester_Id = _ITeacherCoruses.PopulateSemsters();  
  6. // ViewBag.Teacher_Id = new SelectList(db.Registration, "User_Id", "FirstName");  
  7. ViewBag.CurrentSort = sortOrder;  
  8. ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";  
  9. ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";  
  10. if (SearchString != null)  
  11. {  
  12. page = 1;  
  13. }  
  14. else  
  15. {  
  16. SearchString = currentFilter;  
  17. }  
  18. ViewBag.CurrentFilter = SearchString;  
  19. var dept = from s in db.Teacher_courses  
  20. select s;  
  21. dept =dept.Include(t => t.course).Include(t => t.regs).Include(t => t.semesterName).OrderBy(t => t.Teacher_Id);  
  22. if (!String.IsNullOrEmpty(SearchString))  
  23. {  
  24. dept = dept.Where(s => s.course.Course_Name.Contains(SearchString));  
  25. // || s.FirstMidName.Contains(searchString));  
  26. }  
  27. switch (sortOrder)  
  28. {  
  29. case "name_desc":  
  30. dept = dept.OrderByDescending(s => s.course.Course_Name);  
  31. break;  
  32. case "Date":  
  33. dept = dept.OrderBy(s => s.Course_Assignment_date);  
  34. break;  
  35. case "date_desc":  
  36. dept = dept.OrderByDescending(s => s.Course_Assignment_date);  
  37. break;  
  38. default:  
  39. dept = dept.OrderBy(s => s.course.Course_Name);  
  40. break;  
  41. }  
  42. int pageSize = 5;  
  43. int pageNumber = (page ?? 1);  
  44. // var teacher_courses = db.Teacher_courses.Include(t => t.course).Include(t => t.regs).Include(t => t.semesterName).OrderBy(t=>t.Teacher_Id);  
  45. dept = dept.Include(t => t.course).Include(t => t.regs).Include(t => t.semesterName).OrderBy(t => t.Teacher_Id);  
  46. //return View(groupedUsers);  
  47. return View(dept.ToPagedList(pageNumber, pageSize));  
  48. // return View(teacher_courses.ToList());  
  49. }  
below is my view :
  1. @*@model IEnumerable<CMS_Monitoring.Models.Teacher_courses>*@  
  2. @using PagedList;  
  3. @using PagedList.Mvc;  
  4. @using CMS_Monitoring.Helpers;  
  5. @model PagedList.IPagedList<CMS_Monitoring.Models.Teacher_courses>  
  6. @{  
  7. ViewBag.Title = "Index";  
  8. Layout = "~/Views/Shared/_Layout2.cshtml";  
  9. }  
  10. <h2>Index</h2>  
  11. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  
  12. <!-- Required meta tags -->  
  13. <script type="text/javascript">  
  14. setTimeout(function () {  
  15. $('#successMessage').fadeOut('slow');  
  16. }, 12000); // <-- time i  
  17. </script>  
  18. <p>  
  19. @Html.ActionLink("Create New""Create""Departments"new { @class = "btn btn-info" })  
  20. </p>  
  21. @if (TempData["UpdatedMessage"] != null)  
  22. {  
  23. <p class="alert alert-success" id="successMessage">@TempData["UpdatedMessage"]</p>  
  24. }  
  25. <p>  
  26. @Html.ActionLink("Create New""Create")  
  27. </p>  
  28. @using (Html.BeginForm())  
  29. {  
  30. <hr />  
  31. <p>  
  32. Find by Department name: @Html.TextBox("SearchString")  
  33. <input type="submit" value="Search" />  
  34. </p>  
  35. <br /><br /><br /><br />  
  36. if (TempData["DeleteDepartment"] != null)  
  37. {  
  38. <p class="alert alert-success" id="successMessage">@TempData["DeleteDepartment"]</p>  
  39. }  
  40. <table class="table">  
  41. <tr>  
  42. <th>  
  43. @Html.DisplayNameFor(model => model[0].course.Course_Name)  
  44. </th>  
  45. <th>  
  46. @Html.DisplayNameFor(model => model[0].regs.FirstName)  
  47. </th>  
  48. <th>  
  49. @Html.DisplayNameFor(model => model[0].semesterName.Semester_Title)  
  50. </th>  
  51. <th>  
  52. @Html.DisplayNameFor(model => model[0].is_on_Substitute)  
  53. </th>  
  54. <th>  
  55. @Html.DisplayNameFor(model => model[0].Active)  
  56. </th>  
  57. <th>  
  58. @Html.DisplayNameFor(model => model[0].Course_Assignment_date)  
  59. </th>  
  60. <th>  
  61. @Html.DisplayNameFor(model => model[0].Date_Modified)  
  62. </th>  
  63. <th>  
  64. @Html.DisplayNameFor(model => model[0].Added_By)  
  65. </th>  
  66. <th></th>  
  67. </tr>  
  68. @foreach (var item in Model)  
  69. {  
  70. <tr>  
  71. <td>  
  72. @Html.DisplayFor(modelItem => item.course.Course_Name)  
  73. </td>  
  74. <td>  
  75. @Html.DisplayFor(modelItem => item.regs.FirstName)  
  76. </td>  
  77. <td>  
  78. @Html.DisplayFor(modelItem => item.semesterName.Semester_Title)  
  79. </td>  
  80. <td>  
  81. @Html.DisplayFor(modelItem => item.is_on_Substitute)  
  82. </td>  
  83. <td>  
  84. @Html.DisplayFor(modelItem => item.Active)  
  85. </td>  
  86. <td>  
  87. @Html.DisplayFor(modelItem => item.Course_Assignment_date)  
  88. </td>  
  89. <td>  
  90. @Html.DisplayFor(modelItem => item.Date_Modified)  
  91. </td>  
  92. <td>  
  93. @Html.DisplayFor(modelItem => item.Added_By)  
  94. </td>  
  95. <td>  
  96. @Html.ActionLink("Edit""Edit"new { id = item.Id }) |  
  97. @Html.ActionLink("Details""Details"new { id = item.Id }) |  
  98. @Html.ActionLink("Delete""Delete"new { id = item.Id })  
  99. </td>  
  100. </tr>  
  101. }  
  102. </table>  
  103. }  
  104. Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount  
  105. @Html.PagedListPager(Model, page => Url.Action("Index",  
  106. new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
but i need to have one row for each teacher with comma separated courses how to pass this to view and bind can someone show complete process i have group it in controller but do not know how to pass to view and use it .see my view result here 
https://ibb.co/QkMxPf6 

Answers (7)