I am able to pass a Search string and a Sort string parameter from Index.cshtml
to Index
action method in StudentsController
. I have added 3 buttons 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 shown 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
:
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);
}