为什么 sortOrder 参数从视图返回为 null
Why is the sortOrder parameter coming back as null from the View
在调试我的应用程序时,我在操作方法中放置了一个断点,因为排序不起作用。出于某种原因,即使我单击课程 HTML link,sortOrder 参数仍为 null。目标是当用户点击那个 htmllink 时,课程名称应该按降序过滤,这可以来自 switch 语句
动作方法:
[HttpGet]
public ActionResult TranscriptAdmin(int id, string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
StudentMajorRepo sRepo;
MajorRequirmentsRepo mRepo;
GradesRepo gRepo;
RegistrationRepo rRepo;
TranscriptViewModel viewModel = new TranscriptViewModel();
viewModel.StudentId = id;
IList<Grade> AllClasses = new List<Grade>();
IList<Registration> AllRegistrations = new List<Registration>();
IList<Registration> PendingGrades = new List<Registration>();
using (context)
{
sRepo = new StudentMajorRepo(context);
mRepo = new MajorRequirmentsRepo(context);
gRepo = new GradesRepo(context);
rRepo = new RegistrationRepo(context);
viewModel.AllGradesClasses = gRepo.GetAllGradesByUserId(id);
viewModel.StudentMajor = sRepo.GetByStudentId(id);
}
switch (sortOrder)
{
case "name_desc":
viewModel.AllGradesClasses.OrderByDescending(c => c.Registration.CourseSection.Course.CourseTitle);
break;
default: // Name ascending
break;
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(viewModel);
}
剃须刀页面:
<div class="col-md-6">
<table class="table table-striped table-dark">
<thead class="thead-dark">
<tr>
<th>Course Id</th>
<th>@Html.ActionLink("Course", "TranscriptAdmin", "Student", new { studentId = Model.StudentId}, new { sortOrder = ViewBag.NameSortParm })</th>
<th>Credits</th>
<th>Grade</th>
<th>Semester</th>
</tr>
</thead>
将您的 ActionLink
更改为:
<th>@Html.ActionLink("Course", "TranscriptAdmin", "Student", new { studentId = Model.StudentId, sortOrder = ViewBag.NameSortParm}, null)</th>
在调试我的应用程序时,我在操作方法中放置了一个断点,因为排序不起作用。出于某种原因,即使我单击课程 HTML link,sortOrder 参数仍为 null。目标是当用户点击那个 htmllink 时,课程名称应该按降序过滤,这可以来自 switch 语句
动作方法:
[HttpGet]
public ActionResult TranscriptAdmin(int id, string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
StudentMajorRepo sRepo;
MajorRequirmentsRepo mRepo;
GradesRepo gRepo;
RegistrationRepo rRepo;
TranscriptViewModel viewModel = new TranscriptViewModel();
viewModel.StudentId = id;
IList<Grade> AllClasses = new List<Grade>();
IList<Registration> AllRegistrations = new List<Registration>();
IList<Registration> PendingGrades = new List<Registration>();
using (context)
{
sRepo = new StudentMajorRepo(context);
mRepo = new MajorRequirmentsRepo(context);
gRepo = new GradesRepo(context);
rRepo = new RegistrationRepo(context);
viewModel.AllGradesClasses = gRepo.GetAllGradesByUserId(id);
viewModel.StudentMajor = sRepo.GetByStudentId(id);
}
switch (sortOrder)
{
case "name_desc":
viewModel.AllGradesClasses.OrderByDescending(c => c.Registration.CourseSection.Course.CourseTitle);
break;
default: // Name ascending
break;
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(viewModel);
}
剃须刀页面:
<div class="col-md-6">
<table class="table table-striped table-dark">
<thead class="thead-dark">
<tr>
<th>Course Id</th>
<th>@Html.ActionLink("Course", "TranscriptAdmin", "Student", new { studentId = Model.StudentId}, new { sortOrder = ViewBag.NameSortParm })</th>
<th>Credits</th>
<th>Grade</th>
<th>Semester</th>
</tr>
</thead>
将您的 ActionLink
更改为:
<th>@Html.ActionLink("Course", "TranscriptAdmin", "Student", new { studentId = Model.StudentId, sortOrder = ViewBag.NameSortParm}, null)</th>