public ActionResult Edit([Bind(Include = "tForumMessages_messages")] int? id, tForumMessages tForumMessages)
 {
     var t = _db.tForumMessages.Find(id);
     if (t == null) return HttpNotFound();
     t.tForumMessages_messages = WebUtility.HtmlDecode(tForumMessages.tForumMessages_messages);
     t.tUsers_Edit_name = _db.Users.First(a => a.UserName == User.Identity.Name);
     t.tUsers_Edit_datetime = DateTime.Now;
     _db.Entry(t).State = EntityState.Modified;
     _db.SaveChanges();
     return RedirectToAction("Index", new { id = t.tForumThemes.Id, id_list = t.tForumThemes.tForumList.Id });
 }
        public ActionResult Create([Bind(Include = "tForumMessages_messages")] int? id, tForumMessages tForumMessages)
        {
            tForumMessages.tForumThemes = _db.tForumThemes.Find(id);
            tForumMessages.tUsers = _db.Users.First(a => a.UserName == User.Identity.Name);
            tForumMessages.tForumMessages_datetime = DateTime.Now;
            tForumMessages.tForumMessages_hide = false;
            var userId = tForumMessages.tUsers.Id;
            if (tForumMessages.tForumMessages_messages != null)
            {
                tForumMessages.tForumMessages_messages = WebUtility.HtmlDecode(tForumMessages.tForumMessages_messages);
            }
            if (!ModelState.IsValid)
                return RedirectToAction("Index", new {id, id_list = tForumMessages.tForumThemes.tForumList.Id});

            _db.tForumMessages.Add(tForumMessages);
            _db.SaveChanges();
            var r = _db.Roles.ToList();
            foreach (var item2 in r.SelectMany(item => _db.Users.Where(a => a.Roles.Any(b => b.RoleId == item.Id)).Where(a => a.Id != userId)))
            {
                //по пользователям в роли
                if (!_db.tUserNewThemes.Where(a => a.tUsers.Id == item2.Id).Any(b => b.tForumThemes.Id == tForumMessages.tForumThemes.Id))
                {
                    var n = new tUserNewThemes {tForumThemes = tForumMessages.tForumThemes, tUsers = item2};
                    _db.tUserNewThemes.Add(n);
                    _db.SaveChanges();
                }
                //Также новое сообщение
                if (
                    _db.tUserNewMessages.Where(a => a.tUsers.Id == item2.Id)
                        .Any(b => b.tForumMessages.Id == tForumMessages.Id)) continue;
                var nm = new tUserNewMessages {tForumMessages = tForumMessages, tUsers = item2};
                _db.tUserNewMessages.Add(nm);
                _db.SaveChanges();
            }
            var val = Url.RequestContext.HttpContext.Request.Url.Scheme;
            var href = Url.Action("Index", "ForumMessages", new { id = tForumMessages.tForumThemes.Id, id_list = tForumMessages.tForumThemes.tForumList.Id }, val);
            Code.Notify.NewMessage(tForumMessages.Id, href, userId);
            return RedirectToAction("Index", new { id = id, id_list = tForumMessages.tForumThemes.tForumList.Id });
        }
        public ActionResult CreateOffer([Bind(Include = "tOffer_error,tOffer_place,tOffer_tBranch_id,tOffer_tProject_id,tOffer_tReleaseProject_id,tOffer_tReleaseProject_exec_id,tOffer_tSubsystem_id")] 
                                        int id_mess, 
                                        int id_theme, tOffer toffer)
        {
            var val = Url.RequestContext.HttpContext.Request.Url.Scheme;
            var returnUrl = "";
            var href = "";
            if (toffer.tOffer_tReleaseProject_exec_id == 0)
            {
                ModelState.AddModelError("tOffer_tReleaseProject_exec_id", "Укажите версию реализации");
            }
            if (toffer.tOffer_tReleaseProject_id == 0)
            {
                ModelState.AddModelError("tOffer_tReleaseProject_id", "Укажите версию обнаружения");
            }
            if (id_mess != 0)
            {
                var t = _db.tForumMessages.Find(id_mess);
                toffer.tforummessages = t;
                if (!ModelState.IsValid)
                {
                    ViewData["tBranch"] = _db.tBranch.ToList();
                    ViewData["tProject"] = _db.tProject.ToList();
                    ViewData["tReleaseProject"] = _db.tReleaseProject.ToList();
                    ViewData["tReleaseProject_exec"] = _db.tReleaseProject.ToList();
                    ViewData["tSubsystem"] = _db.tSubsystem.ToList();
                    ViewData["id_mess"] = id_mess;
                    ViewData["id_theme"] = id_theme;
                    return View(toffer);
                }
                _db.tOffer.Add(toffer);
                _db.SaveChanges();
                returnUrl = Url.Action("Index", "ForumMessages", new { id = t.tForumThemes.Id, id_list = t.tForumThemes.tForumList.Id });
                href = Url.Action("Index", "ForumMessages", new { id = t.tForumThemes.Id, id_list = t.tForumThemes.tForumList.Id }, val);
            }
            else
            {
                var t = _db.tForumThemes.Find(id_theme);
                toffer.tforumthemes = t;
                if (!ModelState.IsValid)
                {
                    ViewData["tBranch"] = _db.tBranch.ToList();
                    ViewData["tProject"] = _db.tProject.ToList();
                    ViewData["tReleaseProject"] = _db.tReleaseProject.ToList();
                    ViewData["tReleaseProject_exec"] = _db.tReleaseProject.ToList();
                    ViewData["tSubsystem"] = _db.tSubsystem.ToList();
                    ViewData["id_mess"] = id_mess;
                    ViewData["id_theme"] = id_theme;
                    return View(toffer);
                }
                _db.tOffer.Add(toffer);
                _db.SaveChanges();
                returnUrl = Url.Action("Index", "ForumMessages", new { id = t.Id, id_list = t.tForumList.Id });
                href = Url.Action("Index", "ForumMessages", new { id = t.Id, id_list = t.tForumList.Id }, val);
            }
            //и теперь мы создаем замечание - выделим отдельно, так как там много кода
            toffer.tOffer_docnumber =  Create(toffer.Id,User.Identity.Name,href);
            _db.Entry(toffer).State = EntityState.Modified;
            _db.SaveChanges();
            //а теперь допишем сообщение в форум
            var m = new tForumMessages
            {
                tUsers = _db.Users.First(a => a.UserName == User.Identity.Name),
                tForumMessages_datetime = System.DateTime.Now,
                tForumMessages_hide = false,
                tForumMessages_messages =
                    "<p>Создано замечание №" + toffer.tOffer_docnumber + "</p>",
                tForumThemes = id_mess != 0 ? toffer.tforummessages.tForumThemes : toffer.tforumthemes
            };

            _db.tForumMessages.Add(m);
            _db.SaveChanges();

            return RedirectToLocal(returnUrl);
        }