Skip to main content
added 3 characters in body; edited tags
Source Link
Heslacher
  • 50.4k
  • 5
  • 81
  • 175

And I am repeating those similar code pieces in edit and create actions in mvc for all scenarios. So I don't know if there is enough information to make a statement but what would be your suggestions for such cases, what would be the best practice to get rid of those ugly code pieces? Since situations vary, turning them into classes would lead me into so many specific classes which cannot be used for any other situation.

And I am repeating those similar code pieces in edit and create actions in mvc for all scenarios. So I don't know if there is enough information to make a statement but what would your suggestions for such cases, what would be the best practice to get rid of those ugly code pieces? Since situations vary, turning them into classes would lead me into so many specific classes which cannot be used for any other situation.

And I am repeating those similar code pieces in edit and create actions in mvc for all scenarios. So I don't know if there is enough information to make a statement but what would be your suggestions for such cases, what would be the best practice to get rid of those ugly code pieces? Since situations vary, turning them into classes would lead me into so many specific classes which cannot be used for any other situation.

Rollback to Revision 3
Source Link
Mathieu Guindon
  • 75.1k
  • 18
  • 191
  • 463
    //Retrieve all learning centers
    studentAssignmentViewModel.LearningCenters = _learningCenterService.LearningCenters.ToList();
    studentAssignmentViewModel.StudentSelectListItem = studentAssignmentViewModel.LearningCenters
        .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

    //Retrieve All Students
    studentAssignmentViewModel.CoursesOfTeacherStudents = _studentAssignmentService.CoursesOfTeacherStudents.ToList();
        studentAssignmentViewModel.CourseSelectListItemStudentSelectListItem = studentAssignmentViewModel.CoursesOfTeacherStudents
            .Where(ty => (ty.Status != Status.Removed)).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    
    //If the user is not admin, it retrieves the courses belong to the teacher otherwise all courses. DashboardUser is admin!        
    if (userRole == UserRole.DashboardUser) {
            return Json(_studentAssignmentService.StudentCourseRelations().Where(x => (x.Status != Status.Removed))
                .Select(x => new { x.Id, x.Student.TcNo, x.Student.Name, x.Student.LastName, x.CDate, CourseName = x.Course.Name })
                .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        var courseIds = _courseService.Courses
            .Where(x => x.DashboardUserId == _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id).Select(x => x.Id);

        return Json(_studentAssignmentService.StudentCourseRelations().Where(x => (x.Status != Status.Removed) && (courseIds.Contains(x.CourseId)))
            .Select(x => new { x.Id, x.Student.TcNo, x.Student.Name, x.Student.LastName, x.CDate, CourseName = x.Course.Name })
            .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

//Teacher comments on students. Again dashboarduser or teacher check for json which will feed the grid then selectlistitems for the dropdownlistfors

        var userId = _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id;
        var userRole = _dashboardUserAuthenticationService.AuthenticatedDashboardUser.UserRole;
       
        if (userRole == UserRole.DashboardUser) {
            return Json(_studentCommentService.StudentComments.Where(x => (x.Status != Status.Removed))
                .Select(x => new { x.Id, x.Student.Name, x.Student.LastName, x.Comment, Status = x.Status.ToString(), x.MDate }).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        return Json(_studentCommentService.StudentComments_dashboardUserService.Where(x => Get(x.Status != Status.Removed) && x.DashboardUserId == userId)
            .Select(x => new { x.Id, x.Student.Name, x.Student.LastName, x.Comment, Status = x.Status.ToString(),UserRole;
 x.MDate }).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

 if (userRole == UserRole.DashboardUser)
            {
                studentCommentViewModel.StudentSelectListItem = _studentCommentService.Students
                    .Where(y => (y.Status != Status.Removed)).OrderBy(x => x.Name)
                    .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList("1");
            } else {
                var courseIdsstudentAssignmentViewModel.Courses = _courseService_studentAssignmentService.Courses
                    .WhereToList(x => x.DashboardUserId == userId).Select(x => x.Id);
 
                studentCommentViewModelstudentAssignmentViewModel.StudentSelectListItemCourseSelectListItem = _studentCommentServicestudentAssignmentViewModel.StudentsCourses
                    .Where(yt => (yt.Status != Status.Removed) 
                        && (y.StudentCourseRelationst.Any(zTeacherId =>== courseIds.Contains(z.CourseId)))userId).OrderBy(x => x.Name)
                    .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList();
            }

// Course Json to fill the grid then selectlistitems to feed dropdownlistfors

return Json(_courseService.Courses.Include(x => x.ClassRoom).Where(x => x.Status != Status.Removed)
            .Select(x => new {
                x.Id, x.Name, x.BeginningDate, x.EndingDate, ClassRoomName = x.ClassRoom.Name
                , LearningCenterId = x.ClassRoom.LearningCenter.Id, x.MinCapacity, x.MaxCapacity, x.HasTest
                , x.CDate, x.MDate, Status = x.Status.ToString()
            }).ToList()
            .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

//Dropdownlist için bütün kurs merkezlerinin çekilmesi
        courseViewModel.LearningCenters = _courseService.LearningCenters.ToList();
        courseViewModel.LearningCenterSelectListItem = courseViewModel.LearningCenters
            .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)else
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
        //Dropdownlist için bütün sınıfların çekilmesi
        courseViewModelstudentAssignmentViewModel.ClassRoomsCourses = _courseService_studentAssignmentService.ClassRoomsCourses.ToList();
        //Kurs merkezine göre sınıfların filtrelenmesi
        courseViewModelstudentAssignmentViewModel.ClassRoomSelectListItemCourseSelectListItem = courseViewModelstudentAssignmentViewModel.ClassRoomsCourses
            .Where(t => (t.Status != Status.Removed)).OrderBy(x => x.Name)
            .Where(x => Equals(x.LearningCenter, courseViewModel.LearningCenterSelectListItem
                .Select(y => y.Selected)))
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
        //Öğretmenleri getirir
        courseViewModel.Teachers = _courseService.Teachers.ToList();
        courseViewModel.TeacherSelectListItems = courseViewModel.Teachers
            .Where(t => t.Status != Status.Removed).OrderBy(x => x.NameSurname)
            .Select(x => new SelectListItem { Text = x.NameSurname, Value = x.Id.ToString() });
    //Retrieve all learning centers
    studentAssignmentViewModel.LearningCenters = _learningCenterService.LearningCenters.ToList();
    studentAssignmentViewModel.StudentSelectListItem = studentAssignmentViewModel.LearningCenters
        .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

    //Retrieve All Students
    studentAssignmentViewModel.CoursesOfTeacher = _studentAssignmentService.CoursesOfTeacher.ToList();
        studentAssignmentViewModel.CourseSelectListItem = studentAssignmentViewModel.CoursesOfTeacher
            .Where(t => (t.Status != Status.Removed)).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    
    //If the user is not admin, it retrieves the courses belong to the teacher otherwise all courses. DashboardUser is admin!        
    if (userRole == UserRole.DashboardUser) {
            return Json(_studentAssignmentService.StudentCourseRelations().Where(x => (x.Status != Status.Removed))
                .Select(x => new { x.Id, x.Student.TcNo, x.Student.Name, x.Student.LastName, x.CDate, CourseName = x.Course.Name })
                .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        var courseIds = _courseService.Courses
            .Where(x => x.DashboardUserId == _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id).Select(x => x.Id);

        return Json(_studentAssignmentService.StudentCourseRelations().Where(x => (x.Status != Status.Removed) && (courseIds.Contains(x.CourseId)))
            .Select(x => new { x.Id, x.Student.TcNo, x.Student.Name, x.Student.LastName, x.CDate, CourseName = x.Course.Name })
            .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

//Teacher comments on students. Again dashboarduser or teacher check for json which will feed the grid then selectlistitems for the dropdownlistfors

        var userId = _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id;
        var userRole = _dashboardUserAuthenticationService.AuthenticatedDashboardUser.UserRole;
       
        if (userRole == UserRole.DashboardUser) {
            return Json(_studentCommentService.StudentComments.Where(x => (x.Status != Status.Removed))
                .Select(x => new { x.Id, x.Student.Name, x.Student.LastName, x.Comment, Status = x.Status.ToString(), x.MDate }).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        return Json(_studentCommentService.StudentComments.Where(x => (x.Status != Status.Removed) && x.DashboardUserId == userId)
            .Select(x => new { x.Id, x.Student.Name, x.Student.LastName, x.Comment, Status = x.Status.ToString(), x.MDate }).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

 if (userRole == UserRole.DashboardUser)
            {
                studentCommentViewModel.StudentSelectListItem = _studentCommentService.Students
                    .Where(y => (y.Status != Status.Removed)).OrderBy(x => x.Name)
                    .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList();
            } else {
                var courseIds = _courseService.Courses
                    .Where(x => x.DashboardUserId == userId).Select(x => x.Id);
 
                studentCommentViewModel.StudentSelectListItem = _studentCommentService.Students
                    .Where(y => (y.Status != Status.Removed) 
                        && (y.StudentCourseRelations.Any(z => courseIds.Contains(z.CourseId)))).OrderBy(x => x.Name)
                    .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList();
            }

// Course Json to fill the grid then selectlistitems to feed dropdownlistfors

return Json(_courseService.Courses.Include(x => x.ClassRoom).Where(x => x.Status != Status.Removed)
            .Select(x => new {
                x.Id, x.Name, x.BeginningDate, x.EndingDate, ClassRoomName = x.ClassRoom.Name
                , LearningCenterId = x.ClassRoom.LearningCenter.Id, x.MinCapacity, x.MaxCapacity, x.HasTest
                , x.CDate, x.MDate, Status = x.Status.ToString()
            }).ToList()
            .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

//Dropdownlist için bütün kurs merkezlerinin çekilmesi
        courseViewModel.LearningCenters = _courseService.LearningCenters.ToList();
        courseViewModel.LearningCenterSelectListItem = courseViewModel.LearningCenters
            .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
        //Dropdownlist için bütün sınıfların çekilmesi
        courseViewModel.ClassRooms = _courseService.ClassRooms.ToList();
        //Kurs merkezine göre sınıfların filtrelenmesi
        courseViewModel.ClassRoomSelectListItem = courseViewModel.ClassRooms
            .Where(t => t.Status != Status.Removed).OrderBy(x => x.Name)
            .Where(x => Equals(x.LearningCenter, courseViewModel.LearningCenterSelectListItem
                .Select(y => y.Selected)))
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
        //Öğretmenleri getirir
        courseViewModel.Teachers = _courseService.Teachers.ToList();
        courseViewModel.TeacherSelectListItems = courseViewModel.Teachers
            .Where(t => t.Status != Status.Removed).OrderBy(x => x.NameSurname)
            .Select(x => new SelectListItem { Text = x.NameSurname, Value = x.Id.ToString() });
    //Retrieve all learning centers
    studentAssignmentViewModel.LearningCenters = _learningCenterService.LearningCenters.ToList();
    studentAssignmentViewModel.StudentSelectListItem = studentAssignmentViewModel.LearningCenters
        .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

    //Retrieve All Students
    studentAssignmentViewModel.Students = _studentAssignmentService.Students.ToList();
    studentAssignmentViewModel.StudentSelectListItem = studentAssignmentViewModel.Students
        .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    
    //If the user is not admin, it retrieves the courses belong to the teacher otherwise all courses        
    var userId = _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id;
    var userRole = _dashboardUserService.Get(userId).UserRole;
    if (userRole != "1")
    {
        studentAssignmentViewModel.Courses = _studentAssignmentService.Courses.ToList();
        studentAssignmentViewModel.CourseSelectListItem = studentAssignmentViewModel.Courses
            .Where(t => (t.Status != Status.Removed) && t.TeacherId == userId).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    }
    else
    {
        studentAssignmentViewModel.Courses = _studentAssignmentService.Courses.ToList();
        studentAssignmentViewModel.CourseSelectListItem = studentAssignmentViewModel.Courses
            .Where(t => (t.Status != Status.Removed)).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    }
More Example of the same issue
Source Link
mctuna
  • 153
  • 5
    //Retrieve all learning centers
    studentAssignmentViewModel.LearningCenters = _learningCenterService.LearningCenters.ToList();
    studentAssignmentViewModel.StudentSelectListItem = studentAssignmentViewModel.LearningCenters
        .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

    //Retrieve All Students
    studentAssignmentViewModel.StudentsCoursesOfTeacher = _studentAssignmentService.StudentsCoursesOfTeacher.ToList();
        studentAssignmentViewModel.StudentSelectListItemCourseSelectListItem = studentAssignmentViewModel.StudentsCoursesOfTeacher
            .Where(yt => y(t.Status != Status.Removed)).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    
    //If the user is not admin, it retrieves the courses belong to the teacher otherwise all courses. DashboardUser is admin!        
    if (userRole == UserRole.DashboardUser) {
            return Json(_studentAssignmentService.StudentCourseRelations().Where(x => (x.Status != Status.Removed))
                .Select(x => new { x.Id, x.Student.TcNo, x.Student.Name, x.Student.LastName, x.CDate, CourseName = x.Course.Name })
                .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        var courseIds = _courseService.Courses
            .Where(x => x.DashboardUserId == _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id).Select(x => x.Id);

        return Json(_studentAssignmentService.StudentCourseRelations().Where(x => (x.Status != Status.Removed) && (courseIds.Contains(x.CourseId)))
            .Select(x => new { x.Id, x.Student.TcNo, x.Student.Name, x.Student.LastName, x.CDate, CourseName = x.Course.Name })
            .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

//Teacher comments on students. Again dashboarduser or teacher check for json which will feed the grid then selectlistitems for the dropdownlistfors

        var userId = _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id;
        var userRole = _dashboardUserService_dashboardUserAuthenticationService.Get(userId)AuthenticatedDashboardUser.UserRole;
       
        if (userRole == UserRole.DashboardUser) {
            return Json(_studentCommentService.StudentComments.Where(x => (x.Status != "1"Status.Removed))
                .Select(x => new { x.Id, x.Student.Name, x.Student.LastName, x.Comment, Status = x.Status.ToString(), x.MDate }).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        studentAssignmentViewModel}
        return Json(_studentCommentService.CoursesStudentComments.Where(x => (x.Status != _studentAssignmentServiceStatus.CoursesRemoved) && x.ToListDashboardUserId == userId)
            .Select(x => new { x.Id, x.Student.Name, x.Student.LastName, x.Comment, Status = x.Status.ToString(), x.MDate }).ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 

 if (userRole == UserRole.DashboardUser)
    studentAssignmentViewModel        {
                studentCommentViewModel.CourseSelectListItemStudentSelectListItem = studentAssignmentViewModel_studentCommentService.CoursesStudents
                    .Where(ty => (ty.Status != Status.Removed)).OrderBy(x &&=> tx.TeacherIdName)
                    .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList();
            } else {
                var courseIds = _courseService.Courses
                    .Where(x => x.DashboardUserId == userId).Select(x => x.Id);

                studentCommentViewModel.StudentSelectListItem = _studentCommentService.Students
                    .Where(y => (y.Status != Status.Removed) 
                        && (y.StudentCourseRelations.Any(z => courseIds.Contains(z.CourseId)))).OrderBy(x => x.Name)
                    .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList();
            }

// Course Json to fill the grid then selectlistitems to feed dropdownlistfors

return Json(_courseService.Courses.Include(x => x.ClassRoom).Where(x => x.Status != elseStatus.Removed)
            .Select(x => new {
        studentAssignmentViewModel        x.CoursesId, x.Name, x.BeginningDate, x.EndingDate, ClassRoomName = _studentAssignmentServicex.CoursesClassRoom.Name
                , LearningCenterId = x.ClassRoom.LearningCenter.Id, x.MinCapacity, x.MaxCapacity, x.HasTest
                , x.CDate, x.MDate, Status = x.Status.ToString()
            }).ToList()
            .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

//Dropdownlist için bütün kurs merkezlerinin çekilmesi
        courseViewModel.LearningCenters = _courseService.LearningCenters.ToList();
        studentAssignmentViewModelcourseViewModel.CourseSelectListItemLearningCenterSelectListItem = studentAssignmentViewModelcourseViewModel.CoursesLearningCenters
            .Where(ty => y.Status != Status.Removed).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
        //Dropdownlist için bütün sınıfların çekilmesi
        courseViewModel.ClassRooms = _courseService.ClassRooms.ToList();
        //Kurs merkezine göre sınıfların filtrelenmesi
        courseViewModel.ClassRoomSelectListItem = courseViewModel.ClassRooms
            .Where(t => t.Status != Status.Removed)).OrderBy(x => x.Name)
            .Where(x => Equals(x.LearningCenter, courseViewModel.LearningCenterSelectListItem
                .Select(y => y.Selected)))
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
        //Öğretmenleri getirir
        courseViewModel.Teachers = _courseService.Teachers.ToList();
        courseViewModel.TeacherSelectListItems = courseViewModel.Teachers
            .Where(t => t.Status != Status.Removed).OrderBy(x => x.NameSurname)
            .Select(x => new SelectListItem { Text = x.NameSurname, Value = x.Id.ToString() });
    //Retrieve all learning centers
    studentAssignmentViewModel.LearningCenters = _learningCenterService.LearningCenters.ToList();
    studentAssignmentViewModel.StudentSelectListItem = studentAssignmentViewModel.LearningCenters
        .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

    //Retrieve All Students
    studentAssignmentViewModel.Students = _studentAssignmentService.Students.ToList();
    studentAssignmentViewModel.StudentSelectListItem = studentAssignmentViewModel.Students
        .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    
    //If the user is not admin, it retrieves the courses belong to the teacher otherwise all courses        
    var userId = _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id;
    var userRole = _dashboardUserService.Get(userId).UserRole;
    if (userRole != "1")
    {
        studentAssignmentViewModel.Courses = _studentAssignmentService.Courses.ToList();
        studentAssignmentViewModel.CourseSelectListItem = studentAssignmentViewModel.Courses
            .Where(t => (t.Status != Status.Removed) && t.TeacherId == userId).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    }
    else
    {
        studentAssignmentViewModel.Courses = _studentAssignmentService.Courses.ToList();
        studentAssignmentViewModel.CourseSelectListItem = studentAssignmentViewModel.Courses
            .Where(t => (t.Status != Status.Removed)).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    }
    //Retrieve all learning centers
    studentAssignmentViewModel.LearningCenters = _learningCenterService.LearningCenters.ToList();
    studentAssignmentViewModel.StudentSelectListItem = studentAssignmentViewModel.LearningCenters
        .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });

    //Retrieve All Students
    studentAssignmentViewModel.CoursesOfTeacher = _studentAssignmentService.CoursesOfTeacher.ToList();
        studentAssignmentViewModel.CourseSelectListItem = studentAssignmentViewModel.CoursesOfTeacher
            .Where(t => (t.Status != Status.Removed)).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
    
    //If the user is not admin, it retrieves the courses belong to the teacher otherwise all courses. DashboardUser is admin!        
    if (userRole == UserRole.DashboardUser) {
            return Json(_studentAssignmentService.StudentCourseRelations().Where(x => (x.Status != Status.Removed))
                .Select(x => new { x.Id, x.Student.TcNo, x.Student.Name, x.Student.LastName, x.CDate, CourseName = x.Course.Name })
                .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        var courseIds = _courseService.Courses
            .Where(x => x.DashboardUserId == _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id).Select(x => x.Id);

        return Json(_studentAssignmentService.StudentCourseRelations().Where(x => (x.Status != Status.Removed) && (courseIds.Contains(x.CourseId)))
            .Select(x => new { x.Id, x.Student.TcNo, x.Student.Name, x.Student.LastName, x.CDate, CourseName = x.Course.Name })
            .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

//Teacher comments on students. Again dashboarduser or teacher check for json which will feed the grid then selectlistitems for the dropdownlistfors

        var userId = _dashboardUserAuthenticationService.AuthenticatedDashboardUser.Id;
        var userRole = _dashboardUserAuthenticationService.AuthenticatedDashboardUser.UserRole;
       
        if (userRole == UserRole.DashboardUser) {
            return Json(_studentCommentService.StudentComments.Where(x => (x.Status != Status.Removed))
                .Select(x => new { x.Id, x.Student.Name, x.Student.LastName, x.Comment, Status = x.Status.ToString(), x.MDate }).ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }
        return Json(_studentCommentService.StudentComments.Where(x => (x.Status != Status.Removed) && x.DashboardUserId == userId)
            .Select(x => new { x.Id, x.Student.Name, x.Student.LastName, x.Comment, Status = x.Status.ToString(), x.MDate }).ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 

 if (userRole == UserRole.DashboardUser)
            {
                studentCommentViewModel.StudentSelectListItem = _studentCommentService.Students
                    .Where(y => (y.Status != Status.Removed)).OrderBy(x => x.Name)
                    .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList();
            } else {
                var courseIds = _courseService.Courses
                    .Where(x => x.DashboardUserId == userId).Select(x => x.Id);

                studentCommentViewModel.StudentSelectListItem = _studentCommentService.Students
                    .Where(y => (y.Status != Status.Removed) 
                        && (y.StudentCourseRelations.Any(z => courseIds.Contains(z.CourseId)))).OrderBy(x => x.Name)
                    .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).ToList();
            }

// Course Json to fill the grid then selectlistitems to feed dropdownlistfors

return Json(_courseService.Courses.Include(x => x.ClassRoom).Where(x => x.Status != Status.Removed)
            .Select(x => new {
                x.Id, x.Name, x.BeginningDate, x.EndingDate, ClassRoomName = x.ClassRoom.Name
                , LearningCenterId = x.ClassRoom.LearningCenter.Id, x.MinCapacity, x.MaxCapacity, x.HasTest
                , x.CDate, x.MDate, Status = x.Status.ToString()
            }).ToList()
            .ToDataSourceResult(request), JsonRequestBehavior.AllowGet);

//Dropdownlist için bütün kurs merkezlerinin çekilmesi
        courseViewModel.LearningCenters = _courseService.LearningCenters.ToList();
        courseViewModel.LearningCenterSelectListItem = courseViewModel.LearningCenters
            .Where(y => y.Status != Status.Removed).OrderBy(x => x.Name)
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
        //Dropdownlist için bütün sınıfların çekilmesi
        courseViewModel.ClassRooms = _courseService.ClassRooms.ToList();
        //Kurs merkezine göre sınıfların filtrelenmesi
        courseViewModel.ClassRoomSelectListItem = courseViewModel.ClassRooms
            .Where(t => t.Status != Status.Removed).OrderBy(x => x.Name)
            .Where(x => Equals(x.LearningCenter, courseViewModel.LearningCenterSelectListItem
                .Select(y => y.Selected)))
            .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
        //Öğretmenleri getirir
        courseViewModel.Teachers = _courseService.Teachers.ToList();
        courseViewModel.TeacherSelectListItems = courseViewModel.Teachers
            .Where(t => t.Status != Status.Removed).OrderBy(x => x.NameSurname)
            .Select(x => new SelectListItem { Text = x.NameSurname, Value = x.Id.ToString() });
rm tags from title
Link
svick
  • 24.3k
  • 4
  • 52
  • 89
Loading
Tag & typo
Link
RubberDuck
  • 30.9k
  • 6
  • 71
  • 174
Loading
Source Link
mctuna
  • 153
  • 5
Loading