Passing another parameter from Index.cshtml to ActionResult Index in StudentsControler.cs

Hursh 191 Reputation points
2024-06-22T05:07:37.38+00:00

I am able to pass a Search string and a Sort string parameter from Index.cshtml to ActionResult Index in StudentsControler.cs. I have added 3 button and need to pass either True if button1 or False if button2 or NULL if button3. It is much like Search but boolean (True or False) in buttons' case.

How do I do it in the code below?

Index.cshtml:

<div class="btn-group" role="group" aria-label="Filter by IsFeatured">
        <button type="button" class="btn btn-primary @((ViewBag.IsFeaturedFilter == null) ? "active" : "")"
                onclick="location.href='@Url.Action("Index", "Students")'">
            All
        </button>
        <button type="button" class="btn btn-primary @((ViewBag.IsFeaturedFilter == true) ? "active" : "")"
                onclick="location.href='@Url.Action("Index", "Students", new { isFeaturedFilter = true })'">
            Deleted
        </button>
        <button type="button" class="btn btn-primary @((ViewBag.IsFeaturedFilter == false) ? "active" : "")"
                onclick="location.href='@Url.Action("Index", "Students", new { isFeaturedFilter = false })'">
            Not Deleted
        </button>
    </div>

StudentsController.cs:

public ActionResult Index(string sortOrder, string searchString)
        {
            var students = from m in db.Students select m;
            ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            switch (sortOrder)
            {
                case "name_desc":
                    students = subjects.OrderByDescending(s => s.StudentLName );
                    break;
                default:
                    students = subjects.OrderBy(s => s.StudentFName);
                    break;
            }
            if (!String.IsNullOrEmpty(searchString))
            {
                if (!string.IsNullOrEmpty(searchString))
                {
                    students = students .Where(s => s.StudentLName == searchString
                                                || s.StudentFName.Contains(searchString)
                                                );
                }
            }
            ViewBag.CurrentSearch = searchString;
            return View(students);
        }

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,325 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,389 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 27,341 Reputation points
    2024-06-22T11:51:11.0166667+00:00

    I've provided a solution in your previous post.

    https://learn.microsoft.com/en-us/answers/questions/1722861/issue-with-view-actionresult-on-button-click-in-mv

    Please do not create duplicate posts. If there is a problem with the solution in your previous post then explain why the code does not meet your expectations.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. SurferOnWww 2,326 Reputation points
    2024-06-22T08:04:04.4166667+00:00

    I have added 3 button and need to pass either True if button1 or False if button2 or NULL if button3.

    Please write your code to show how you have added the buttons, explain what your issue is, and let us know what you need to know to solve the issue, while keeping in mind that many answers have already been given to you in the previous questions:

    Filter Records in ASP.NET MVC

    Issue with View ActionResult on Button Click in MVC 5

    0 comments No comments