The following code works fine and display student enrollment records on the page. Right before the table element, I have 3 buttons to filter out records based on the button that was clicked. In the controller, I added a function IsEnrolled
. The first button “All” calls Index
and it works fine. The second button “Enrolled” call IsEnrolled
function and I get an error:
The view ‘IsEnrolled’ or its master was not found or no view engine supports the searched locations.
What do I need to do to make it to work?
@model IEnumerable<MyProject.Models.MyModel>
@{
ViewBag.Title = "Index";
}
<h2>Class Enrollment</h2>
<form method="get" action="@Url.Action(" Index")">
<p>
Search by Last Name: <input type="text" name="searchString" value="@ViewData[" CurrentFilter"]" />
<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", "MyModel" )'">
All
</button>
<button type="button" class="btn btn-primary @((ViewBag.IsFeaturedFilter == true) ? " active" : "" )"
onclick="location.href='@Url.Action(" IsEnrolled "MyModel" , new { isFeaturedFilter=true })'">
Enrolled
</button>
<button type="button" class="btn btn-primary @((ViewBag.IsFeaturedFilter == false) ? " active" : "" )"
onclick="location.href='@Url.Action(" Index", "MyModel" , new { isFeaturedFilter=false })'">
Not Enrolled
</button> </div> </form>
<table class="table">
<tr style="background-color:lightsteelblue; color:navy">
<th>
@Html.DisplayNameFor(model => model.StudentId)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.EnrollmentStatus)
</th>
</tr>
@{
int i = 0;
}
<tr class="@rowColor">
<td>
@Html.DisplayFor(modelItem => item.StudentID)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName, new { @class = "form-control", @readonly = "readonly" })
</td>
<td>
@Html.DisplayFor(modelItem => item.EnrollmentStatus)
</td>
</tr>
}
</table>
<br />
Code:
namespace MyProject.Controllers
{
public class StudentsController : Controller
{
private cnStudents db = new cnStudents();
public ActionResult Index(string sortOrder, string searchString)
{
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)
);
}
}
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);
}
}
}