Skip to content

Commit

Permalink
Added searchString param to Razor Pages web app
Browse files Browse the repository at this point in the history
  • Loading branch information
shahedc committed May 3, 2020
1 parent 451a585 commit 45cc2f3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
37 changes: 22 additions & 15 deletions src/NetLearner.Pages/Pages/ResourceLists/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page
@page "{searchString?}"
@model NetLearner.Pages.IndexModel

@{
Expand All @@ -10,6 +10,12 @@
<p>
<a asp-page="Create">Create New List</a>
</p>
<form>
<p>
Search: <input type="text" asp-for="SearchString" />
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
Expand All @@ -20,19 +26,20 @@
</tr>
</thead>
<tbody>
@foreach (var item in Model.ResourceList) {
<tr>
<td>
<a href="/LearningResources/@item.Id">
@Html.DisplayFor(modelItem => item.Name)
</a>
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
@foreach (var item in Model.ResourceList)
{
<tr>
<td>
<a href="/LearningResources/@item.Id">
@Html.DisplayFor(modelItem => item.Name)
</a>
</td>
<td>
<a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
<a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
12 changes: 11 additions & 1 deletion src/NetLearner.Pages/Pages/ResourceLists/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@ public IndexModel(NetLearner.SharedLib.Data.LibDbContext context)
_context = context;
}

[BindProperty(SupportsGet = true)]
public string SearchString { get; set; }

public IList<ResourceList> ResourceList { get;set; }

public async Task OnGetAsync()
{
ResourceList = await _context.ResourceLists.ToListAsync();
var resourceLists = from rs in _context.ResourceLists
select rs;
if (!string.IsNullOrEmpty(SearchString))
{
resourceLists = resourceLists.Where(s => s.Name.Contains(SearchString));
}

ResourceList = await resourceLists.ToListAsync();
}
}
}

0 comments on commit 45cc2f3

Please sign in to comment.