protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL server using (comp2007Entities db = new comp2007Entities()) { //use the Student model to save the new record Course c = new Course(); int courseID = 0; if (Request.QueryString["CourseID"] != null) { courseID = int.Parse(Request.QueryString["CourseID"]); c = (from obj in db.Courses where obj.CourseID == courseID select obj).FirstOrDefault(); } c.Title = txtTitle.Text; c.Credits = int.Parse(txtCredits.Text); c.DepartmentID = int.Parse(ddlDepartment.SelectedValue); if (courseID == 0) { db.Courses.Add(c); } db.SaveChanges(); //redirect to the updated students page Response.Redirect("courses.aspx"); } }
public ActionResult CreateCourse(FormCollection data) { // Validate Form Data // basic validation not using model binding if (data["courseName"].IsNullOrWhiteSpace()|| data["courseNumber"].IsNullOrWhiteSpace()) { ViewBag.errors = "Both the Course name and number are required"; return View(); } // Create course var course = new Course { CourseId = data["courseNumber"], Name = data["courseName"] }; // Create the Application state HttpContext.Application.Lock(); HttpContext.Application["course"] = course; HttpContext.Application.UnLock(); // Set the session sort state on new session or if missing exisitng sort date if (Session.IsNewSession || Session["sort"] == null) { Session["sort"] = "id"; } // redirect back to the page index return RedirectToAction("Index"); }