I needed help with this code
Model:
public class SearchList
{
public string Name { get; set; }
public string className { get; set; }
}
Controller:
[Authorize]
public IActionResult Search(string sortOrder, string currentFilter, string name, int? page, string className)
{
ViewData["CurrentSort"] = sortOrder;
ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
var searchList = FetchList()
if (name != null)
{
page = 1;
}
else
{
name = currentFilter;
}
ViewData["CurrentFilter"] = name;
if (!String.IsNullOrEmpty(name))
{
searchList = searchList.Where(m => m.Name.Contains(name)).ToList();
}
switch (sortOrder)
{
case "name_desc":
searchList = searchList.OrderByDescending(s => s.Name).ToList();
break;
default:
break;
}
int pageSize = 3;
return View(PaginatedList<SearchList>.Create(searchList, page ?? 1, pageSize));
}
public IList<SearchList> FetchList()
{
var searchList = new List<SearchList>();
searchList.Add(new SearchList
{
Name = "Andy",
className = "Class Alpha",
});
searchList.Add(new SearchList
{
Name = "Nando",
className = "Class Beta",
});
searchList.Add(new SearchList
{
Name = "Anrya",
className = "Class Alpha",
});
return searchList;
}
My View
<form asp-action="Search">
<div>
<table id="t1" class="table table-bordered table-condensed"> <tr>
<th>
<b>Details</b>
</th>
</tr>
<tr>
<td >
Name
</td>
<td>
<input class="form-control" type="text" name="name" value="@ViewData["currentFilter"]" />
</td>
</tr>
<tr>
<td>
Class Name
</td>
<td>
<input class="form-control" style="width: 200px" type="text" name="classname">
</td>
</tr>
</table>
<div class="form-group">
<input type="submit" value="Search" class="btn btn-default"/>
</div>
</div>
</form>
}
</div>
</div>
<form>
<div>
<table>
<tr>
<td colspan="6" class="tableheader">
Listing</b>
</td>
</tr>
<tr style="font-weight: bold; background:#efefef">
<td>
@Html.ActionLink("Name", "Search", new { sortOrder = ViewBag.NameSortParm })
</td>
<td>
Class Name
</td>
</tr>
@{
foreach (var m in Model.AsEnumerable())
{
<tr class="oddeventable">
<td>
@Html.ActionLink(@m.Name, "TestStudent", "Student", new { Name = m.Name })
</td>
<td>
@m.ClassName
</td>
</tr>
}
}
</table>
</div>
</form>
@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}
<a asp-action="Search"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex - 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @prevDisabled">
Previous
</a>
<a asp-action="Search"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex + 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @nextDisabled">
Next
</a>
The current view and controller only able to received one search string "Name", what I wanted to do is that it can search for className as well, so basically the controller will retrieve the name and className and filter it and create the new searchList and post back to the View. I am quite lost actually because the 'currentFilter' is the 'name' itself so I am not sure how to tweak it that the filter can accept two search strings so later in the View I could click on "Next" page or Sorting while having the currentFilter.
Looks like you already have className accepted as a parameter. I would remove the currentFilter parameter and Add/change your filtering:
Also change your inputs so the value is defaulted by the correct ViewData item. Note you are not currently setting the value on className.
And remove from your next/prev:
However, you could instead change your method to accept currentFilterName and currentFilterClassName but as you are accessing the ViewData anyway, it seems unnecessary.