I am trying to put a simple ASP.Net Core 3 MVC Application together and have followed several tutorials in getting a single input box to search against a single SQL Server table column which works fine, sadly I am a little lacking in knowledge on how to perform the next part....
My question is:
How do I put together a search where I might have several input boxes against a single column, or several input boxes against several columns?
i.e.
input1, input2, input3, input4, input5, input6 on the view, to be able to search against SQL Database table dbo.Products on the Column/Field "Description"?
Ideally, all input boxes can be left blank or be filled in to filtered data.
The reason I require several search boxes against a column is because the 'description' field can contain a lot of information that is in no particular order.
i.e.
Description: product category, product name, material1, material 2, material 3, material 4, size, rating, colour, thickness
The existing Access front-end has several search boxes so a user can filter previous purchased items:
name, size, colour or rating, material 4, product name.
The tutorial set has the following lambda expression to perform the search:
View = Views/Raptors/Index.cshtml
<form asp-controller="Raptors" asp-action="Index">
<p>
Position: <input type="text" name="searchString" />
<input type="submit" value="Filter" />
College: <input type="text" name="searchCollege" />
<input type="submit" value="Filter" />
</p>
</form>
Controller = Controllers/RaptorsController
// GET: Raptors
//public async Task<IActionResult> Index() // hidden to add search
public async Task<IActionResult> Index(string searchString)
{
var raptor = from m in _context.Raptors select m; // linq query alternative to sql query
// added for the search function
if (!String.IsNullOrEmpty(searchString)) // if it's not empty...
{
raptor = raptor.Where(s => s.PlayerPosition.Contains(searchString));
}
// end of search function
// return View(await _context.Raptors.ToListAsync()); // hidden to add search
return View(await raptor.ToListAsync());
}
Being honest, I am still trying to get my head around what is actually going on in the controller, how things refer to each other. That is part of my battle here.
The updated example is more geared to two search boxes for two fields respectively, but I'd still like to be able to search one field by more than one search box because of all the variables in that one field.
I have been able to use the two search boxes in my example but they both have to be filled in, ideally I would like to be able to search using any of the input boxes blank or otherwise.