コード例 #1
0
        public ActionResult New(Evaluation model)
        {
            if (model.Questions.Where(q => q.SelectedAnswer == null && q.Required == "Y").Count() > 0)
            {
                throw new Exception("尚有*字號題目未填寫!!");
            }
            if (ModelState.IsValid)
            {
                QuestMain main = new QuestMain();
                main.Docid   = model.Docid;
                main.YYYYmm  = model.YYYYmm;
                main.Rtt     = DateTime.Now;
                main.CustId  = model.CustId;
                main.CustNam = _context.Departments.Find(model.CustId).Name_C;
                main.Qtitle  = _context.QuestionnaireMs.Find(model.Id).Qname;
                _context.QuestMains.Add(main);
                //
                List <QuestAnswer> at = _context.QuestAnswers.Where(a => a.Docid == model.Docid).ToList();
                QuestAnswer        ar;
                foreach (QuestAnswer w in at)
                {
                    if (w != null)
                    {
                        _context.QuestAnswers.Remove(w);
                    }
                }
                foreach (var q in model.Questions)
                {
                    // Save the data
                    ar        = new QuestAnswer();
                    ar.Docid  = model.Docid;
                    ar.VerId  = q.ID;
                    ar.Qid    = q.QID;
                    ar.Answer = q.SelectedAnswer;
                    _context.QuestAnswers.Add(ar);
                }
                try
                {
                    _context.SaveChanges();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }

                return(new JsonResult(at)
                {
                    Value = new { success = true, error = "" }
                });
            }
            else
            {
                string msg = "";
                foreach (var error in ViewData.ModelState.Values.SelectMany(modelState => modelState.Errors))
                {
                    msg += error.ErrorMessage + Environment.NewLine;
                }
                throw new Exception(msg);
            }
        }
コード例 #2
0
 public override void OnEnter()
 {
     base.OnEnter();
     main = Owner.GetComponent <QuestMain>();
 }