Commit e61fc53f authored by Andres Käver's avatar Andres Käver
Browse files

search

parent bd462c68
......@@ -5,49 +5,53 @@
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<h1>Recipes</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<form class="form-inline" method="get">
<label asp-for="SearchName" class="my-1 mr-2"></label>
<input asp-for="SearchName" class="form-control mb-2 mr-sm-2"/>
<label asp-for="SearchDescription" class="my-1 mr-2"></label>
<input asp-for="SearchDescription" class="form-control mb-2 mr-sm-2"/>
<input asp-for="Btn" type="submit" value="Search" class="btn btn-primary mb-2 my-1"/>
@if (!string.IsNullOrWhiteSpace(Model.SearchName) || !string.IsNullOrWhiteSpace(Model.SearchDescription))
{
<input asp-for="Btn" type="submit" value="Reset" class="btn btn-primary mr-2 mb-2 my-1"/>
}
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].Instructions)
</th>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].Servings)
</th>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].PrepTime)
</th>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].Category)
</th>
<th></th>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].PrepTime)
</th>
<th>
@Html.DisplayNameFor(model => model.Recipe[0].Category)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Recipe) {
@foreach (var item in Model.Recipe)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Instructions)
</td>
<td>
@Html.DisplayFor(modelItem => item.Servings)
@item.Description.Substring(0, item.Description.Length - 1)
</td>
<td>
@Html.DisplayFor(modelItem => item.PrepTime)
......@@ -61,6 +65,6 @@
<a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
}
</tbody>
</table>
</table>
\ No newline at end of file
......@@ -21,10 +21,43 @@ namespace WebApp.Pages.Recipes
public IList<Recipe> Recipe { get;set; } = default!;
public async Task OnGetAsync()
[BindProperty(SupportsGet = true)]
public string? SearchName { get; set; }
[BindProperty(SupportsGet = true)]
public string? SearchDescription { get; set; }
[BindProperty(SupportsGet = true)]
public string? Btn { get; set; }
public async Task<IActionResult> OnGetAsync()
{
Recipe = await _context.Recipes
.Include(r => r.Category).ToListAsync();
var query = _context.Recipes
.Include(r => r.Category).AsQueryable();
if (Btn == "Reset")
{
SearchName = "";
SearchDescription = "";
Btn = "";
return RedirectToPage("/Recipes/Index");
}
SearchName = SearchName?.Trim();
if (!string.IsNullOrWhiteSpace(SearchName))
{
query = query.Where(m => m.Name.Contains(SearchName));
}
SearchDescription = SearchDescription?.Trim();
if (!string.IsNullOrWhiteSpace(SearchDescription))
{
query = query.Where(m => m.Description.Contains(SearchDescription));
}
Recipe = await query.ToListAsync();
return Page();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment