コード例 #1
0
 public ActionResult Create()
 {
     try
     {
         ExamList model = new ExamList();
         model.InitNewObject();
         PrepareViewBag();
         return(View("Create", model));
     }
     catch (Exception ex)
     {
         ViewBag.Message = ex.Message;
         return(Index());
     }
 }
コード例 #2
0
        public ActionResult SaveCreated(ExamList model)
        {
            try
            {
                model.InitNewObject();
                string msg = model.Validate();
                if (!string.IsNullOrEmpty(msg))
                {
                    ViewBag.Message = msg;
                    PrepareViewBag();
                    return(View("Create", model));
                }
                using (DataContext ctx = new DataContext())
                {
                    var students = ctx.StudentGroup
                                   .First(t => t.Id == model.StudentGroupId)
                                   .Students;

                    foreach (User student in students)
                    {
                        model.ExamListDetail.Add(new ExamListDetail()
                        {
                            Student  = student,
                            ExamList = model
                        });
                    }

                    model.ExamDate = DateTime.Now;
                    ctx.ExamList.Add(model);

                    ctx.SaveChanges();
                    return(RedirectToAction("Edit", new { id = model.Id }));
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex.Message;
                PrepareViewBag();
                return(View("Create", model));
            }
        }