Home > Back-end >  Why is the sortOrder parameter coming back as null from the View
Why is the sortOrder parameter coming back as null from the View

Time:03-11

When debugging my application I put a breakpoint in the Action Method as the sorting was not working. For some reason, the sortOrder parameter keeps coming in as null even though I click on the Course HTML link. The goal is when a user clicks that htmllink the course names should be filtered by descending order which can been from the switch statement

ActionMethod:

[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);
}

razor page:

<div >
    <table >
        <thead >
            <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>

CodePudding user response:

Change your ActionLink to:

<th>@Html.ActionLink("Course", "TranscriptAdmin", "Student", new { studentId = Model.StudentId, sortOrder = ViewBag.NameSortParm}, null)</th>
  • Related