Beispiel #1
0
        public ActionResult Create([Bind(Include = /*ID,*/ "LastName,FirstName,EnrollmentDate")] Student student)
        {//Bind-include--mono auta tha perasoun(whitelist)---security ths method,prostateuw to model mou apo kakovoules energeies(security best practisies)
         //bind one level of defensive programming

            //defensive technic h try
            //neos tropos gia apothhkeysh sthn DB
            try   //breakpoint edw an ftasei edw o kwdikas exw server side validation (opou exw C# einai serverside)
            {
                if (ModelState.IsValid)
                {
                    db.Students.Add(student);   //to kanei add se mia lista linq
                    // entity elegxei entities kai markarei(ws ADDED) tn student afou ginei add sto epomeno db.saveChenges
                    //An ginei allagh status:modified k sto epomeno SaveChanges
                    //By default UNCHANGED
                    //Entity status: ADDED,UNCHANGED,MODIFIED,DETACHED(ADIAFORIA APO TO ENTITY),DELETED
                    db.SaveChanges();           //to pernaei sthn DB
                    return(RedirectToAction("Index"));
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save Changes");
            }

            return(View(student));
        }
Beispiel #2
0
        public ActionResult Create([Bind(Include = "Lastname,Firstname,EnrollmentDate")] Student student)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Students.Add(student);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save Changes");
            }


            return(View(student));
        }
        public ActionResult Create([Bind(Include = "CourseID,Title,Hours,DepartmentID")] Course course)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Courses.Add(course);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Unable to save Changes");
            }


            //ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", course.DepartmentID);
            PopulateDepartmentsDropDownList();
            return(View(course));
        }
Beispiel #4
0
        public ActionResult Create([Bind(Include = "LastName,FirstName,HireDate, OfficeAssignment")] Instructor instructor, string[] selectedCourses)
        {
            //fill courses list on Instructor
            if (selectedCourses != null)
            {
                instructor.Courses = new List <Course>();
                foreach (var course in selectedCourses)
                {
                    var courseToAdd = db.Courses.Find(int.Parse(course));       //int.parse gt h find fernei int---to course einai to course id
                    instructor.Courses.Add(courseToAdd);
                }

                if (ModelState.IsValid)
                {
                    db.Instructors.Add(instructor);
                    db.SaveChanges();       //Instructor kai meta courses
                    return(RedirectToAction("Index"));
                }
            }
            PopulateAssignedCourseData(instructor);
            return(View(instructor));
        }