asp.net mvc - Searching always returns empty list in MVC? -


i'm trying implement filtering in page contains data, page shows 3 different entities: branches, items , categories, used view model:

   public class warehousedata     {         public ienumerable<item> items { get; set; }         public ienumerable<category> categories { get; set; }         public ienumerable<branch> branches { get; set; }     } 

in controller:

public actionresult index(string sort, string search)         {             var warhouse = new warehousedata();             warhouse.items = db.items.include(c => c.categories).tolist();             warhouse.branches = db.branches;             viewbag.search = search;              warhouse.branches = db.branches.tolist();             switch (sort)             {                 case "q_asc":                     warhouse.items = warhouse.items.orderby(c => c.quantity).tolist();                     viewbag.sort = "q_desc";                     break;                 case "q_desc":                     warhouse.items = warhouse.items.orderbydescending(c => c.quantity).tolist();                     viewbag.sort = "q_asc";                     break;                 default:                     warhouse.items = warhouse.items.orderby(c => c.name).tolist();                     viewbag.sort = "q_asc";                     break;             }              if (!string.isnullorempty(search))             {                 warhouse.items = warhouse.items.where(i => i.name.contains(search)).tolist();                 viewbag.search = search;             }             return view(warhouse);         } 

this index view:

@model warehousemanagementmvc.viewmodels.warehousedata  @{     viewbag.title = "index"; }  <h2 style="display:inline-block">registered branches | </h2> @html.actionlink("add new branch", "create", controllername: "branch") @foreach (var branch in model.branches) {     <ul>         <li>@html.actionlink(branch.location, "details", "branch", routevalues: new { id = branch.id }, htmlattributes: null)</li>     </ul> } <hr /> <h2>all items available</h2> <div>     @using (@html.beginform("index", "warehouse", formmethod.get))     {         <input type="text" name="search" value="@viewbag.search"/>         <input type="submit" value="filter" />     } </div> <table class="table table-bordered">     <tr>         <th>product</th>         <th>@html.actionlink("quantity", "index", new { sort = viewbag.sort, search = viewbag.search })</th>         <th>categories</th>     </tr>      @foreach (var item in model.items)     {         <tr>             <td>                  @html.actionlink(item.name, "details", "item", routevalues: new { id = item.id }, htmlattributes: null)               </td>             <td>                 <span>@item.quantity</span>             </td>             <td>                 @{foreach (var cat in item.categories)                     {                         @cat.name <br />                     }                 }             </td>         </tr>     } </table> 

but when search, result empty list, why?

there's 2 things cause items empty here:

  1. there's no items in db.
  2. none of items have name values contain search term. advised in particular here contains case-sensitive. if name foo , searched foo it not match. if want case-insensitive search, should cast both name , search either lowercase or uppercase before comparing:

    where(m => m.name.tolower().contains(search.tolower())) 

while we're here. search should ideally filter @ database level. currently, you're selecting items in database , filtering them in-memory, highly-inefficient. you call tolist() in second line of action, query has been sent, have in order set warhouse.items. instead, should store items in temporary variable, i.e. iqueryable items = db.items.include(c => c.categories); , conditional ordering , filtering on that. then, finally, set warhouse.items = items.tolist();.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -