Пример #1
0
        public object Edit([Bind(Prefix = "")]Preference preference, string course,
		                   string prof, string CourseList)
        {
            ViewData["Title"] = "Edit Preference";
            ViewData["Message"] = "Edit a Preference";

            // redisplay form immediately if there are input format errors
            if (!ViewData.ModelState.IsValid)
                return View(preference);
            int profId;
            try {
                profId = Convert.ToInt32(prof);
                ViewData["id"] = prof;
            } catch (Exception) {  //not an integer
                return RedirectToAction("Index", "Professor");
            }
            int courseId;
            try {
                courseId = Convert.ToInt32(course);
            } catch (Exception) { //not an integer
                return this.RedirectToAction(c => c.Show(prof));
            }
            try {
                var prefProv = new PreferencesProvider();
                Preference pref = prefProv.GetPreferenceByProfIdAndCourseId(profId, courseId);
                if (pref != null) {
                    Course cour = (new CourseProvider()).GetCourseByID(Convert.ToInt32(CourseList));
                    pref.Id.Course = cour;
                    pref.Weight = preference.Weight;
                    prefProv.UpdatePreference(pref);
                    return View("Saved");
                }
                return this.RedirectToAction(c => c.Show(prof));
            } catch (RuleViolationException vex) {
                ViewData.ModelState.CopyValidationExceptions(vex);
                PrepareCoursesList(preference);
                return View("Edit", preference);
            } catch (Exception err) {
                ViewData.ModelState.CopyValidationExceptions(err, "preference");
                PrepareCoursesList(preference);
                return View("Edit", preference);
            }
        }
Пример #2
0
        public ActionResult Delete(string prof, string course)
        {
            int profId, courseId;
            try {
                profId = Convert.ToInt32(prof);
                ViewData["id"] = prof;
            } catch (Exception)   //not an integer
            {
                return RedirectToAction("Index", "Professor");
            }
            try {
                courseId = Convert.ToInt32(course);
            } catch (Exception)   //not an integer
            {
                return this.RedirectToAction(c => c.Show(prof));
            }

            try {
                var prefProvider = new PreferencesProvider();
                Preference preference = prefProvider.GetPreferenceByProfIdAndCourseId(profId, courseId);
                if (preference != null) {
                    prefProvider.DeletePreference(preference);
                    return View("Deleted");
                }
                return this.RedirectToAction(c => c.Show(prof));
            } catch (Exception err) {
                if (err.InnerException != null &&
                    err.InnerException.Message.Contains(
                        "The DELETE statement conflicted with the REFERENCE constraint")) {
                            ViewData["ErrorMessage"] =
                                "Course could not be deleted.. there are assoiciated data.. cannot delete";
                        } else {
                            ViewData["ErrorMessage"] = "Preference Could not be deleted.. there is a problem";
                        }
                return View("NotDeleted");
            }
        }