I have basically created a modified MVC template that is handling CRUD operations for some reports. What I am trying to accomplish is filtering the reports based on multiple inputs. I have managed to create a basic search function that basically can search every column in the database (except from date which I still havent completely figured out), but I want three textboxes, each representing its own column, for filtering the rows. One for the column identifying the report, one for the date (maybe two so I can search between dates), and one for the customer.
This is the code in the controller:
public ActionResult Avvik(string searchAWB, string searchMottaker)
{
var rapports = db.Rapports.Include(r => r.Årsak);
if (!String.IsNullOrEmpty(searchAWB))
{
rapports = rapports.Where(r => r.AWB.Contains(searchAWB));
}
else if (!String.IsNullOrEmpty(searchMottaker))
{
rapports = rapports.Where(r => r.Mottaker.Contains(searchMottaker));
}
return View("Rapporter/Avvik", rapports.ToList());
}
And this is some of the code in the view:
@Html.ActionLink("Back to admin tools", "Adminpanel", "Admin")
@using (Html.BeginForm())
{
<p>AWB: @Html.TextBox("SearchAWB")
Mottaker: @Html.TextBox("SeachRecipient")
<input type="submit" value="Filter"/></p>
}
I have tried some variants of the OR (||) operator in the if/else and ended up with the code above. Only the first if-statement is working as intended.
Might be you want below code. which will retrieve records after checking both filter value.
rapports = rapports.Where(r => (String.IsNullOrEmpty(searchAWB) || r.AWB.Contains(searchAWB))
&& (String.IsNullOrEmpty(searchMottaker) || r.Mottaker.Contains(searchMottaker)));
If you need all records which just satisfy any one condition use below code
rapports = rapports.Where(r => (String.IsNullOrEmpty(searchAWB) || r.AWB.Contains(searchAWB))
|| (String.IsNullOrEmpty(searchMottaker) || r.Mottaker.Contains(searchMottaker)));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.