public ActionResult Index(PlayersIndexModel playersIndexModel) { using (tennisContext db = new tennisContext()) { if (Request.Method == "GET") { if (HttpContext.Session.GetString("PlayerSearch") != null) { playersIndexModel.PlayerSearch = HttpContext.Session.GetString("PlayerSearch"); } } if (playersIndexModel.PlayerSearch != null) { playersIndexModel.Players = db.Player .Include("Country") .Select(player => player) .Where(p => p.Name.ToUpper().Contains(playersIndexModel.PlayerSearch.ToUpper())) .ToList() .OrderBy(p => p.Name.Substring(Math.Max(p.Name.LastIndexOf(' '), 0)).ToUpper()) .ToList(); } else { playersIndexModel.Players = db.Player .Include("Country") .Select(player => player) .ToList() .OrderBy(p => p.Name.Substring(Math.Max(p.Name.LastIndexOf(' '), 0)).ToUpper()) .ToList(); } if (Request.Method == "POST") { if (playersIndexModel.PlayerSearch == null) { HttpContext.Session.SetString("PlayerSearch", ""); } else { HttpContext.Session.SetString("PlayerSearch", playersIndexModel.PlayerSearch); } } } return(View(playersIndexModel)); }