0

I am very new to MVC and software development, and I want to implement a search by date functionality within my application. On the index view I want users to be able to search by two date fields that are available on the web page, "Date" and "Order Date". Currently this is the code I have in the view, it basically specifies the layout of the page.

using (Html.BeginForm("Index","PurchaseOrder",FormMethod.Get))
{
    <p>
        Search by Date: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Html.TextBox("searchBy","dateSearchBegin") to: @Html.TextBox("searchBy","dateSearchEnd")
        <input type="submit" value="Search" /> <br /><br />
        Search by Order Date: &nbsp;@Html.TextBox("searchBy","dateOrderedBegin") to: @Html.TextBox("searchBy","dateOrderedEnd")
        <input type="submit"  value="Search" />
        <br /> <br />
        Search by: @Html.RadioButton("searchBy","Requestor", true) Requestor
        @Html.RadioButton("searchBy","Vendor") Vendor
        @Html.RadioButton("searchBy","workOrder") Work Order # <br />
        @Html.TextBox("search","", new { style = "width:10000px" })
        <input type="submit" value="Search"  />
    </p>

}

In the controller, I have this code. This is where I am trying to provide the logic to be able to filter by different fields.

// GET: PurchaseOrder
public ActionResult Index(string searchBy, string search,  int? page)
{

    if (searchBy == "Requestor")
    {
        return View(db.PurchaseOrders.Where(x => x.Requestor.ToUpper().Contains(search)).OrderBy(i => i.DateOrdered).ToPagedList(page ?? 1, 15));
    }
    else if (searchBy == "Vendor")
    {
        return View(db.PurchaseOrders.Where(x => x.Vendor.ToUpper().Contains(search)).OrderBy(i=>i.DateOrdered).ToPagedList(page??1,15));
    }
    else if (searchBy == "workOrder")
    {
        return View(db.PurchaseOrders.Where(x => x.PurchaseRequest_.ToUpper().Contains(search)).OrderBy(i => i.DateOrdered).ToPagedList(page ?? 1, 15));
    }
    else if (searchBy == "dateSearchBegin")
    {
        return View(db.PurchaseOrders.Where(x => x.Date > "dateSearchBegin" && x.Date < "dateSearchEnd").OrderBy(i => i.Date).ToPagedList(page ?? 1,15));
    }
    else if (searchBy == "dateSearchEnd")
    {
        return View(db.PurchaseOrders.Where(x => x.Date > "dateOrderedBegin" && x.Date < "dateOrderedEnd").OrderBy(i => i.Date).ToPagedList(page ?? 1,15));
    }
    int pageSize = 15;
    int pageNumber = (page ?? 1);
    pageNumber = page == null ? (ViewBag.pageData == null ? 1 : (int)ViewBag.pageData) : page.Value;
    ViewBag.pageData = page;
    return View(db.PurchaseOrders.OrderBy(i => i.DateOrdered).ToPagedList(pageNumber, pageSize));
}

I am getting an error message that states "operator '<' cannot be applied to operands of type 'System.DateTime?' and 'string'. How can I fix this error, and is there a more efficient way to code this to achieve what I am trying to do?

2
  • you are comparing DateTime object with string Commented Aug 18, 2014 at 13:08
  • x.Date > "dateSearchBegin" you're comparing types DateTime and string which isn't valid or logical Commented Aug 18, 2014 at 13:08

1 Answer 1

1

You need to construct DateTime object from query (string search) and then use it in linq query.

var dtFrom = DateTime.Parse(search)
var dtTo ...

return View(db.PurchaseOrders.Where(x => x.Date > dtFrom && x.Date < dtTo).OrderBy(i => i.Date).ToPagedList(page ?? 1,15));

But you need also to introduce the ability to search by multiple properties at once. You can achieve this by only returning IQueryable from the linq queries and do subsequent queries on it. This way you can concatenate multiple queries:

var filtered = db.PurchaseOrders.Where(x => ...).AsQueryable(); 
filtered = filtered.Where(x => ...).AsQueryable();

etc

and then return

filtered.ToPagedList()
Sign up to request clarification or add additional context in comments.

3 Comments

How do you set the textbox in the view equal to the dtFrom variable?
search is an empty string value
You can send the model to the view along with data, the model would contain all the fields for searching

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.