예제 #1
0
        // POST api/QuestionAdmin
        public HttpResponseMessage PostQuestion(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Questions.Add(question);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, question);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = question.QuestionId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
        public ActionResult Create(Question question)
        {
            /*Question q = new Question();
            q.QuestionText = question.GetValue("QuestionText").AttemptedValue;
            q.AnswerA = question.GetValue("AnswerA").AttemptedValue;
            q.AnswerB = question.GetValue("AnswerB").AttemptedValue;
            q.AnswerC = question.GetValue("AnswerC").AttemptedValue;
            q.AnswerD = question.GetValue("AnswerD").AttemptedValue;
            q.Correct = Int16.Parse(question.GetValue("Correct").AttemptedValue);
            q.CategoryId = Int16.Parse(question.GetValue("CategoryId").AttemptedValue);
            q.Category = db.Categories.Find(q.CategoryId);

            db.Questions.Add(q);
            db.SaveChanges();
            return RedirectToAction("Index");*/

            question.CategoryId = question.Category.CategoryId;
            question.Category = db.Categories.Find(question.Category.CategoryId);
            db.Questions.Add(question);
            db.SaveChanges();
            return RedirectToAction("Index");

               /* try
            {
                //PopulateCategoryDropDownList();
                var errors = ModelState.Values.SelectMany(v => v.Errors);
                if (ModelState.IsValid)
                {
                    db.Questions.Add(question);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException dex)
            {
                ModelState.AddModelError("",dex.Message);
            }
               // ViewBag.categoryId = new SelectList(db.Categories, "CategoryId", "Name", question.Category.CategoryId);
            PopulateCategoryDropDownList(question.Category.CategoryId);
            return View(question);*/
        }
예제 #3
0
        // PUT api/QuestionAdmin/5
        public HttpResponseMessage PutQuestion(int id, Question question)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != question.QuestionId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(question).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        public ActionResult Edit(Question question)
        {
            /*question.CategoryId = question.Category.CategoryId;
            question.Category = db.Categories.Find(question.Category.CategoryId);
            db.Entry(question).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");*/

            if (ModelState.IsValid)
            {
                db.Entry(question).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }