public AdminQuestion(Question q) { this.id = q.Id; this.Index = q.Index; this.shortText =_context.Questions.Where(x => x.Id == this.id).First().Title .Replace("<b>", String.Empty) .Replace("</b>", String.Empty) .Replace("<ul>", String.Empty) .Replace("<li>", String.Empty); if (this.shortText.Length > 100) { this.shortText = this.shortText.Substring(0, 100) + "..."; } Category thisCategory = null; if (q.CategoryId == -1) this.CategoryName = "INVALID"; else { thisCategory = _context.Categories.First(x => x.Id == q.CategoryId); this.CategoryName = thisCategory.Title; } //Questionnaire index is calculated in awful way QuestionnaireIndex = 0; _context.Categories.Where(x => x.Index < thisCategory.Index).ToList().ForEach(x => QuestionnaireIndex += x.Questions.Count()); QuestionnaireIndex += thisCategory.Questions.Where(x => x.Index <= Index).Count(); }
public ActionResult Create(Question question) { if (ModelState.IsValid) { //Always set a new question as the first question in its category. The user can change the order of the question after it is saved question.Index = 1; _context.Questions.Add(question); Question entry = _context.Entry(question).Entity; _context.SaveChanges(); return RedirectToAction("Edit/" + question.Id); } return View(question); }
/////CREATE QUESTION//////////////////// public ActionResult Create() { Question q = new Question(); q.Index = _context.Questions.Count() + 1; return View(q); }
private void shiftIndexes(Question entry, int oldIndex, System.Data.Entity.DbSet<Question> dbset) { if (entry.Index - oldIndex == 1) { //Move forward by 1 step try { dbset.First(x => x.Index == entry.Index).Index = oldIndex; //If was last row - it will throw an exception } catch (Exception e) { } } else if (entry.Index > oldIndex) { //We moved forward foreach (var q in dbset.ToList()) { if (oldIndex < q.Index && q.Index < entry.Index) q.Index = q.Index - 1; } entry.Index = entry.Index - 1; } else if (entry.Index < oldIndex) { //We moved backwards foreach (var q in dbset.ToList()) { if (entry.Index <= q.Index && q.Index < oldIndex) q.Index = q.Index + 1; } } if (entry.Index > dbset.Count()) entry.Index = dbset.Count(); else if (entry.Index < 1) entry.Index = 1; }
public ActionResult Edit(Question question) { if (ModelState.IsValid) { _context.Entry(question).State = EntityState.Modified; _context.SaveChanges(); //return RedirectToAction("Index"); // Question entry = _context.Entry(question).Entity; // System.Data.Entity.DbSet<Question> dbset = _context.Questions; //If we changed index, we need to shift other elements // int oldIndex = dbset.First(x => x.Id == entry.Id).Index; // shiftIndexes(entry, oldIndex, dbset); //Ugly workaround //Question dbQ = dbset.First(x => x.Id == entry.Id); //dbQ.Index = entry.Index; //dbQ.CategoryId = question.CategoryId; //dbQ.PostAnnotation = entry.PostAnnotation; //dbQ.Summary = entry.Summary; //dbQ.Title = entry.Title; //_context.SaveChanges(); return RedirectToAction("Index"); } return View(question); }