这是我的控制器中的一个动作 - 在我的整个项目中,我的控制器中有类似的大型方法。
我正在尝试学习将这些东西放在哪里以及如何清理它们。我是这样做的新手,如果我看到一个关于如何更改我自己的代码的好例子,它可能会教我如何对大量代码执行此操作。
这是我的行动:
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.TitleSortParm = String.IsNullOrEmpty(sortOrder) ? "Title desc" : "";
ViewBag.CreditsSortParm = sortOrder == "Credits" ? "Credits desc" : "Credits";
ViewBag.ElectiveSortParm = sortOrder == "Elective" ? "Elective desc" : "Elective";
ViewBag.InstructorSortParm = sortOrder == "Instructor" ? "Instructor desc" : "Instructor";
ViewBag.YearSortParm = sortOrder == "Year" ? "Year desc" : "Year";
ViewBag.AttendingDaysSortParm = sortOrder == "AttendingDays" ? "AttendingDays desc" : "AttendingDays";
ViewBag.AttendanceCapSortParm = sortOrder == "AttendanceCap" ? "AttendanceCap desc" : "AttendanceCap";
ViewBag.StartDateSortParm = sortOrder == "StartDate" ? "StartDate desc" : "StartDate";
ViewBag.LocationSortParm = sortOrder == "Location" ? "Location desc" : "Location";
ViewBag.ParishSortParm = sortOrder == "Parish" ? "Parish desc" : "Parish";
ViewBag.DescriptionSortParm = sortOrder == "Description" ? "Description desc" : "Description";
ViewBag.ApprovedSortPArm = sortOrder == "Approved" ? "Approved desc" : "Approved";
ViewBag.CompletedSortPArm = sortOrder == "Completed" ? "Completed desc" : "Completed";
ViewBag.ArchivedSortPArm = sortOrder == "Archived" ? "Archived desc" : "Archived";
if (Request.HttpMethod == "GET")
{
searchString = currentFilter;
}
else
{
page = 1;
}
ViewBag.CurrentFilter = searchString;
var courses = from s in db.Courses
select s;
if (!String.IsNullOrEmpty(searchString))
{
courses = courses.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "Title desc":
courses = courses.OrderByDescending(s => s.Title);
break;
case "Credits":
courses = courses.OrderBy(s => s.Credits);
break;
case "Credits desc":
courses = courses.OrderByDescending(s => s.Credits);
break;
case "Elective":
courses = courses.OrderBy(s => s.Credits);
break;
case "Elective desc":
courses = courses.OrderByDescending(s => s.Credits);
break;
case "Instructor":
courses = courses.OrderBy(s => s.Instructor.LastName);
break;
case "Instructor desc":
courses = courses.OrderByDescending(s => s.Instructor.LastName);
break;
case "Year":
courses = courses.OrderBy(s => s.Year);
break;
case "Year desc":
courses = courses.OrderByDescending(s => s.Year);
break;
case "AttendingDays":
courses = courses.OrderBy(s => s.AttendingDays);
break;
case "AttendingDays desc":
courses = courses.OrderByDescending(s => s.AttendingDays);
break;
case "AttendanceCap":
courses = courses.OrderBy(s => s.AttendanceCap);
break;
case "AttendanceCap desc":
courses = courses.OrderByDescending(s => s.AttendanceCap);
break;
case "StartDate":
courses = courses.OrderBy(s => s.StartDate);
break;
case "StartDate desc":
courses = courses.OrderByDescending(s => s.StartDate);
break;
case "Location":
courses = courses.OrderBy(s => s.Location);
break;
case "Location desc":
courses = courses.OrderByDescending(s => s.Location);
break;
case "Parish":
courses = courses.OrderBy(s => s.Parish);
break;
case "Parish desc":
courses = courses.OrderByDescending(s => s.Parish);
break;
case "Description":
courses = courses.OrderBy(s => s.Description);
break;
case "Description desc":
courses = courses.OrderByDescending(s => s.Description);
break;
case "Approved":
courses = courses.OrderBy(s => s.Approved);
break;
case "Approved desc":
courses = courses.OrderByDescending(s => s.Approved);
break;
case "Completed":
courses = courses.OrderBy(s => s.Completed);
break;
case "Completed desc":
courses = courses.OrderByDescending(s => s.Completed);
break;
case "Archived":
courses = courses.OrderBy(s => s.Archived);
break;
case "Archived desc":
courses = courses.OrderByDescending(s => s.Archived);
break;
default:
courses = courses.OrderBy(s => s.Title);
break;
}
int pageSize = 4;
int pageNumber = (page ?? 1);
return View(courses.ToPagedList(pageNumber, pageSize));
}
我应该对上面的代码做些什么以使其更具可读性?我是否只是将它的一部分作为单独的方法移出并将它们移到控制器的底部?我是否将这些方法放在另一个文件中的某个地方并在此处引用它?
请记住我正在学习并且很享受清晰。