// GET: TrainingPrograms public ActionResult Index(string _pastprograms) { using (SqlConnection conn = Connection) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { if (_pastprograms == "true") { cmd.CommandText = @"SELECT * FROM TrainingProgram WHERE StartDate <= getdate();"; } else { cmd.CommandText = @"SELECT * FROM TrainingProgram WHERE StartDate >= getdate();"; } SqlDataReader reader = cmd.ExecuteReader(); TrainingProgramIndexViewModel toTheModel = new TrainingProgramIndexViewModel(); List <TrainingProgram> trainingPrograms = new List <TrainingProgram>(); while (reader.Read()) { TrainingProgram trainingProgram = new TrainingProgram { Id = reader.GetInt32(reader.GetOrdinal("id")), Name = reader.GetString(reader.GetOrdinal("name")), StartDate = reader.GetDateTime(reader.GetOrdinal("startDate")), EndDate = reader.GetDateTime(reader.GetOrdinal("endDate")), MaxAttendees = reader.GetInt32(reader.GetOrdinal("maxAttendees")) }; trainingPrograms.Add(trainingProgram); } toTheModel.TrainingPrograms = trainingPrograms; if (_pastprograms == "true") { toTheModel.PastProgram = true; } else if (_pastprograms == "false") { toTheModel.PastProgram = false; } reader.Close(); return(View(toTheModel)); } } }
// GET: TrainingProgram public async Task <IActionResult> Index() { using (IDbConnection conn = Connection) { string sql = @"SELECT tp.Id, tp.[Name], tp.StartDate, tp.EndDate, tp.MaxAttendees FROM TrainingProgram tp ORDER BY tp.Id"; IEnumerable <TrainingProgram> trainingPrograms = await conn.QueryAsync <TrainingProgram>(sql); TrainingProgramIndexViewModel viewModel = new TrainingProgramIndexViewModel(); viewModel.TrainingPrograms = trainingPrograms.Where(tp => tp.StartDate > DateTime.Today).ToList(); return(View(viewModel)); } }