c# - Filtering database search with multiple textboxes with LINQ lambda in MVC .NET -


i have created modified mvc template handling crud operations reports. trying accomplish filtering reports based on multiple inputs. have managed create basic search function can search every column in database (except date still havent figured out), want 3 textboxes, each representing own column, filtering rows. 1 column identifying report, 1 date (maybe 2 can search between dates), , 1 customer.

this code in 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 of code in view:

@html.actionlink("back 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 variants of or (||) operator in if/else , ended code above. first if-statement working intended.

might want below code. 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 need records satisfy 1 condition use below code

 rapports = rapports.where(r =>  (string.isnullorempty(searchawb) || r.awb.contains(searchawb))  || (string.isnullorempty(searchmottaker) || r.mottaker.contains(searchmottaker))); 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -