Exemple #1
0
        public ActionResult CreateQuestion(string editorContent, string tagSelect, string stringTitle)
        {
            var appliedTagList = JsonConvert.DeserializeObject<string[]>(tagSelect);
            int ptId = 0;
            if (ModelState.IsValid)
            {
                Question asking = new Question();

                asking.Title = stringTitle;
                while (editorContent.EndsWith("<br>"))
                    editorContent = editorContent.Substring(0, editorContent.Length - 4);
                asking.Content = editorContent.Trim('\r', '\n', ' ');
                //article.UserId =  User.Identity.GetUserId<int>();
                asking.UserId =  User.Identity.GetUserId<int>();
                asking.CreateDate = DateTime.Now;
                asking.isActive = true;
                //asking. = 0;

                db.Questions.Add(asking);
                db.SaveChanges();
                ptId = asking.Id;
                if (appliedTagList != null)
                {
                    foreach (var item in appliedTagList)
                    {
                        AppliedTagQuestion appliedTag = new AppliedTagQuestion();
                        appliedTag.QuestionId = asking.Id;
                        appliedTag.TagId = Int32.Parse(item);
                        //appliedTag.TaggingUser =  User.Identity.GetUserId<int>();
                        appliedTag.AppliedDate = DateTime.Now;
                        db.AppliedTagQuestions.Add(appliedTag);
                        db.SaveChanges();
                    }
                }
            }

            return Json(ptId, JsonRequestBehavior.AllowGet);
        }
Exemple #2
0
        public ActionResult EditQuestion(string questionId, string editorContent, string tagSelect, string stringTitle)
        {
            int id = Int32.Parse(questionId);
            var appliedTagList = JsonConvert.DeserializeObject<string[]>(tagSelect);
            if (ModelState.IsValid)
            {
                Question asking = (from qt in db.Questions
                                   where qt.Id == id && qt.isActive == true
                                   select qt).FirstOrDefault();

                asking.Title = stringTitle;
                while (editorContent.EndsWith("<br>"))
                    editorContent = editorContent.Substring(0, editorContent.Length - 4);
                asking.Content = editorContent.Trim('\r', '\n', ' ');
                asking.CreateDate = DateTime.Now;
                asking.isActive = true;
                //asking. = 0;

                db.Entry(asking).State = EntityState.Modified;
                db.SaveChanges();

                // Delete applied tags in question
                var appliedTags = (from apTag in db.AppliedTagQuestions
                                   where apTag.QuestionId == id
                                   select apTag).ToList();
                foreach (var item in appliedTags)
                {
                    var delTag = db.AppliedTagQuestions.Where(a => a.QuestionId == id
                                                                        && a.TagId == item.TagId).Single();
                    db.AppliedTagQuestions.Remove(delTag);
                }
                db.SaveChanges();
                //Save Applied tags
                if (appliedTagList != null)
                {
                    foreach (var item in appliedTagList)
                    {
                        AppliedTagQuestion appliedTag = new AppliedTagQuestion();
                        appliedTag.QuestionId = asking.Id;
                        appliedTag.TagId = Int32.Parse(item);
                        //appliedTag.TaggingUser =  User.Identity.GetUserId<int>();
                        appliedTag.AppliedDate = DateTime.Now;
                        db.AppliedTagQuestions.Add(appliedTag);
                        db.SaveChanges();
                    }
                }
            }
            return Json(id, JsonRequestBehavior.AllowGet);
        }