Below is a working example using your original code with several bug fixes.
Students Controller (with mock DbContext)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcBasic.Controllers
{
public class cnStudents
{
public cnStudents()
{
Students = new List<Student>() {
new Student()
{
StudentID = 1,
EnrollmentDate = DateTime.Now.AddMonths(-5),
FirstName = "Foo",
LastName = "Bar",
IsEnrolled = true
},
new Student()
{
StudentID = 2,
EnrollmentDate = DateTime.Now.AddMonths(-3),
FirstName = "Les",
LastName = "Paul",
IsEnrolled = false
},
new Student()
{
StudentID = 3,
EnrollmentDate = DateTime.Now.AddMonths(-2),
FirstName = "Leo",
LastName = "Fender",
IsEnrolled = true
},
new Student()
{
StudentID = 4,
EnrollmentDate = DateTime.Now,
FirstName = "Slash",
LastName = "",
IsEnrolled = false
}
};
}
public List<Student> Students { get; set; }
public void Dispose() { }
}
public class Student
{
public Student() {}
public int StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime EnrollmentDate { get; set; }
public bool IsEnrolled { get; set; }
}
public class StudentsController : Controller
{
private cnStudents db = new cnStudents();
public ActionResult Index(string sortOrder, string searchString, bool? isFeaturedFilter = null)
{
var subjects = from m in db.Students select m;
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
switch (sortOrder)
{
case "name_desc":
subjects = subjects.OrderByDescending(s => s.FirstName);
break;
subjects = subjects.OrderByDescending(s => s.LastName);
break;
case "Date":
subjects = subjects.OrderBy(s => s.EnrollmentDate);
break;
}
if (!String.IsNullOrEmpty(searchString))
{
if (!string.IsNullOrEmpty(searchString))
{
subjects = subjects.Where(s => s.LastName == searchString
|| s.FirstName.Contains(searchString)
|| s.FirstName.ToString().Contains(searchString)
);
}
}
if (isFeaturedFilter != null)
{
subjects = subjects.Where(s => s.IsEnrolled == isFeaturedFilter);
ViewBag.IsFeaturedFilter = isFeaturedFilter;
}
ViewBag.CurrentSearch = searchString;
return View(subjects);
}
//public ActionResult IsEnrolled(bool? isFeaturedFilter = null)
//{
// var students = from m in db.Students select m;
// students = students.Where(s => s.IsEnrolled == isFeaturedFilter);
// ViewBag.IsFeaturedFilter = isFeaturedFilter;
// return View(subjects);
//}
public ActionResult SearchAct()
{
return View();
}
[HttpPost]
public ActionResult SearchAct(string nameToFind)
{
ViewBag.SearchKey = nameToFind;
return View();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Index View
@model IEnumerable<MvcBasic.Controllers.Student>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<form method="get" action="@Url.Action("Index")">
<p>
Search by Last Name: <input type="text" name="searchString" value="@ViewBag.CurrentSearch" />
<input type="submit" value="Search" class="btn btn-default" /> @*| <a asp-action="Index">Go Back</a>*@
@Html.ActionLink("Show All", "Index")
</p>
<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 })'">
Enrolled
</button>
<button type="button" class="btn btn-primary @((ViewBag.IsFeaturedFilter == false) ? " active" : "" )"
onclick="location.href='@Url.Action("Index", "Students" , new { isFeaturedFilter=false })'">
Not Enrolled
</button>
</div>
</form>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.EnrollmentDate)
</th>
<th>
@Html.DisplayNameFor(model => model.IsEnrolled)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EnrollmentDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsEnrolled)
</td>
</tr>
}
</table>