I'm working on a tutorial for ASP.Net and in the tutorial we are building a basic search form to search a list. When I am running the code, if I type a string or character into the search bar, I don't get any results. I figured out that if I go to the URL it says .../Restaurants/List?searchTerm=+Ja, If I change the url to say .../Restaurants/List?searchTerm=Ja it successfully searches the term. (I searched "Ja"). I'm wondering why it adds the + and how to make it so it doesn't add that, so it successfully searches the wanted term. Check out some code below.
list.cshtml.cs:
... ...
public class ListModel : PageModel
{
private readonly IConfiguration config;
private readonly IRestaurantData restaurantData;
public string Message { get; set; }
public IEnumerable<Restaurant> Restaurants { get; set; }
public ListModel(IConfiguration config, IRestaurantData restaurantData)
{
this.config = config;
this.restaurantData = restaurantData;
}
public void OnGet(string searchTerm)
{
Message = config["Message"];
Restaurants = restaurantData.GetRestaurantsByName(searchTerm);
}
}
}
list.cs.html:
@page
@model ListModel
@{
}
<h1>Restaurants</h1>
<form method="get">
<div class="form-group">
<div class="input-group">
<input type="search" class="form-control" value=" " name="searchTerm">
<span class="input-group-btn">
<button class="btn btn-default">
Search
</button>
</span>
</div>
</div>
</form>
<table class="table">
@foreach(var restaurant in Model.Restaurants)
{
<tr>
<td>@restaurant.Name</td>
<td>@restaurant.Location</td>
<td>@restaurant.Cuisine</td>
</tr>
}
</table>
<div>@Model.Message</div>