c# - how to filter between dates in ASP.NET Web Application -
i new mvc , software development, , want implement search date functionality within application. on index view want users able search 2 date fields available on web page, "date" , "order date". code have in view, specifies layout of page.
using (html.beginform("index","purchaseorder",formmethod.get)) { <p> search date: @html.textbox("searchby","datesearchbegin") to: @html.textbox("searchby","datesearchend") <input type="submit" value="search" /> <br /><br /> search order date: @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 controller, have code. trying provide logic able filter 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 getting error message states "operator '<' cannot applied operands of type 'system.datetime?' , 'string'. how can fix error, , there more efficient way code achieve trying do?
you need construct datetime
object query (string search
) , use 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 need introduce ability search multiple properties @ once. can achieve returning iqueryable
linq queries , subsequent queries on it. way can concatenate multiple queries:
var filtered = db.purchaseorders.where(x => ...).asqueryable(); filtered = filtered.where(x => ...).asqueryable();
etc
and return
filtered.topagedlist()
Comments
Post a Comment