Iβve been developing my first large (for me) MVC project for a couple of months, and everything becomes extremely difficult to navigate.
I refuse refactoring and I am looking for modern examples of "best practices" because your controller is thin and moves all this data into your models.
I am reading this article , which discusses things in detail, but does not provide an example project.
The most popular "best practices" topics posted here tend to be linked to the MVC Music Store or the Nerd Dinner project, but at the same time, comments tend to say they are more of a "beginner's guide" rather than examples of "best practices" "
Does anyone know of any modern open source MVC projects that demonstrate the right development structure?
note: a typical problem I would like to study: my controllers are very long and full of code that controls the website - I need to translate this code into methods that the controller simply refers to. Where can I use all of these methods?
Here is an example of my code from a controller, as suggested by a comment on one of the answers. How to move some of this information into my ViewModel? (I have included ViewModel below):
Controller:
public ActionResult AttendanceView(int id) { // // Generates list of Attendances specifically for current Course var attendanceItems = db.Attendance.Where(s => s.CourseID == id); List<Attendance> attendanceItemsList = attendanceItems.ToList(); // End of generating list of Attendances // // Generates list of Students in alphabetical order sorted by LastName var student = attendanceItemsList.Select(a => a.Student).Distinct().OrderBy(s => s.LastName); List<Student> StudentList = student.ToList(); // End of generating list of Students // // Generates list of AttendingDays specifically for current Course Course course = db.Courses.FirstOrDefault(p => p.CourseID == id); List<int> attDayList = new List<int>(); for (int i = 0; i < course.AttendingDays; i++) { attDayList.Add(i + 1); }; // End of generating list of AttendingDays AttendanceReportViewModel model = new AttendanceReportViewModel { AttendanceDays = attDayList, Students = StudentList, Attendances = attendanceItemsList, courseId = id }; return View(model); }
ViewModel:
namespace MyApp.ViewModels { public class AttendanceReportViewModel { public List<int> AttendanceDays { get; set; } public List<Student> Students { get; set; } public List<Attendance> Attendances { get; set; } public int courseId { get; set; } public string IsPresent(Student student, int attendanceDay) { return Attendances.Single(a => a.StudentID == student.StudentID && a.AttendanceDay == attendanceDay).Present ? MyAppResource.Present_Text : MyAppResource.Absent_Text; } } }
source share