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.

Related posts

Recent Viewed