c# - sorting and searching of data in table not working -


i follow tutorials on sorting , searching from:

http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

when try , sorting not working. want sorting on 3 fields, username, user_role , date_of_register. displayed data not sorting in order (just displaying data). please can me doing wrong.

//controller code:     public actionresult displayusers(string sortorder, string searchstring)         {             viewbag.namesortparm1 = string.isnullorempty(sortorder) ? "name_desc" : "";             viewbag.namesortparm2 = string.isnullorempty(sortorder) ? "role_desc" : "";             viewbag.datesortparm = sortorder == "date" ? "date_desc" : "date";              var users = in db.app_user                             select a;              switch (sortorder)             {                 case "name_desc":                     users = users.orderbydescending(a => a.username);                     break;                 case "role_desc":                     users = users.orderby(a => a.user_role);                     break;                 case "date_desc":                     users = users.orderby(a => a.date_of_register);                     break;                 default:                     users = users.orderby(a => a.username);                     break;             }              return view(db.app_user.tolist());         }  

view page

//displayusers.cshtml page     <table class="table">         <thead>             <tr>                 <th>                     @html.actionlink("user name", "displayusers", new { sortorder = viewbag.namesortparm1 })                 </th>                 <th>                     @html.actionlink("user role", "displayusers", new { sortorder = viewbag.namesortparm2 })                 </th>                 <th>                     @html.displaynamefor(model => model.is_active)                                 </th>                 <th>                     @html.actionlink("enrollment date", "displayusers", new { sortorder = viewbag.datesortparm })                 </th>                 <th></th>             </tr>         </thead>          @foreach (var item in model)         {             <tr>                 <td>                     @html.displayfor(modelitem => item.username)                 </td>                 <td>                     @html.displayfor(modelitem => item.user_role)                 </td>                 <td>                     @html.displayfor(modelitem => item.is_active)                 </td>                 <td>                     @html.displayfor(modelitem => item.date_of_register)                 </td>                         </tr>         }     </table> 

when did: return view(users.tolist()); sorting based on field user name not no user role , enrollment date. can not sort data on basis of user role or enrollment date why so?

for searching: tried searcing on username , date_of_register field. when tried code:

if (!string.isnullorempty(searchstring))     {         users = users.where(a => a.username.contains(searchstring) || a.date_of_register.contains(searchstring));     } 

getting error:

error 1 instance argument: cannot convert 'system.datetime?' 'system.linq.iqueryable'

error 2 'system.datetime?' not contain definition 'contains' , best extension method overload 'system.linq.queryable.contains(system.linq.iqueryable, tsource)' has invalid arguments

how can make search on date field? please me this, on these highly appreciated.

sorting:

the problem you're using prefetched list you're not sending view.

return view(db.app_user.tolist());  

should

return view(user.tolist()); 

searching:

you're again using contains expects object queryable while datetime not, need write special case datetime filter:

datetime datetime = convert.todatetime(searchstring); if (datetime.timeofday != timespan.zero) //comparison 00:00:00 time {     filterresults = filterresults.where(x => x.timestarted == datetime); } else {     datetime plusonedatetime = datetime.adddays(1).date;     filterresults = filterresults.where(x => x.timestarted >= datetime.date                                             && x.timestarted < plusonedatetime); } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -