c# - Sorting with the Entity Framework in an ASP.NET MVC Application -
i have index action method passing result , , want sort table through same index action method how can pass both value view. here index action method
public actionresult index(string sortorder) { var usercount = db.countuser(); homecoming view(usercount.tolist()); }
and here sorting code:
viewbag.namesortparm = string.isnullorempty(sortorder) ? "name_desc" : ""; var teams = t in db.teams select t; switch (sortorder) { case "name_desc": teams = teams.orderbydescending(t => t.teamname); break; default: teams = teams.orderby(t => t.teamname); break; }
you can accomplish by:
public actionresult index(string sortorder) { var usercount = db.countuser(); viewbag.namesortparm = string.isnullorempty(sortorder) ? "name_desc" : sortorder; var teams = t in db.teams select t; switch ((string)viewbag.namesortparm) { case "name_desc": teams = teams.orderbydescending(t => t.teamname); break; default: teams = teams.orderby(t => t.teamname); break; } viewbag.teams = teams.tolist(); homecoming view(usercount.tolist()); }
and utilize viewbag.teams
in view.
however, proper solution think creating proper model contains both collections:
public class myindexviewmodel { public list<countuser> countusers { get; set; } public list<team> teams { get; set; } } public actionresult index(string sortorder) { var usercount = db.countuser(); viewbag.namesortparm = string.isnullorempty(sortorder) ? "name_desc" : sortorder; var teams = t in db.teams select t; switch ((string)viewbag.namesortparm) { case "name_desc": teams = teams.orderbydescending(t => t.teamname); break; default: teams = teams.orderby(t => t.teamname); break; } homecoming view(new myindexviewmodel { countusers = usercount.tolist(), teams = teams.tolist() }); }
and need alter model type in view myindexviewmodel
c# asp.net-mvc
No comments:
Post a Comment