Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the sortOrder parameter coming back as null from the View

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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>

>Solution :

Change your ActionLink to:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading