public ActionResult Create(Lesson lesson, string FCKeditor) { if (LessonsDAL.existLesson(lesson.LessonName)) ModelState.AddModelError("LessonName", "Съществува урок с това име"); if (String.IsNullOrEmpty(lesson.LessonName)) ModelState.AddModelError("LessonName", "Въведете име на урока"); if (ModelState.IsValid) { if (lesson.LessonName.Length > 50) ModelState.AddModelError("LessonName", "Името на урока трябва да е под 50 символа"); if (ModelState.IsValid) { lesson.UserId = UsersDAL.getUserId(User.Identity.Name); DateTime datetime = DateTime.Now; var lessonName = datetime.ToString("yyyyMMddHHmmssfff"); StreamWriter sr = System.IO.File.CreateText(HttpContext.Server.MapPath(@"~\Content\Lessons\Lesson" + lessonName + ".txt")); using (sr) { sr.Write(FCKeditor); } sr.Dispose(); lesson.Code = lessonName; db.Lessons.Add(lesson); db.SaveChanges(); return RedirectToAction("Index"); } } ViewBag.SubcategoryId = new SelectList(db.Subcategories, "SubcategoryId", "SubcategoryName", lesson.SubcategoryId); ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName", lesson.UserId); return View(lesson); }
public LessonsViewModel(Lesson lesson) { this.LessonId = lesson.LessonId; this.LessonName = lesson.LessonName; this.LessonCode = lesson.Code; }
public ActionResult Edit(Lesson lesson, string FCKeditor) { string lessonName = LessonsDAL.getLessonName(lesson.LessonId); if (lesson.LessonId != (int)TempData["LessonId"]) { ModelState.AddModelError(String.Empty, "Грешно ID"); } if (lesson.UserId != (Guid)TempData["UserId"]) { ModelState.AddModelError(String.Empty, "Грешно ID"); } if (String.IsNullOrEmpty(lessonName)) ModelState.AddModelError("LessonName", "Въведете име на урока"); if (ModelState.IsValid) { var lessonNameC = LessonsDAL.getLessonCode(lesson.LessonId); StreamWriter sr = System.IO.File.CreateText(HttpContext.Server.MapPath(@"~\Content\Lessons\Lesson" + lessonNameC + ".txt")); sr.Write(FCKeditor); sr.Dispose(); lesson.LessonName = lessonName; lesson.Code = lessonNameC; db.Entry(lesson).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.SubcategoryId = new SelectList(db.Subcategories, "SubcategoryId", "SubcategoryName", lesson.SubcategoryId); ViewBag.UserId = new SelectList(db.Users, "UserId", "UserName", lesson.UserId); return View(lesson); }