Esempio n. 1
0
        public ActionResult Create([Bind(Include = "TopicPostID,Body")] TopicPost post, int? Topics, string TopicName)
        {
            Topic topic = db.Topics.Find(Topics);            
            Member poster = db.Users.Find(User.Identity.GetUserId());

            if (poster == null)
                ModelState.AddModelError("Poster", "It seems you don't exist. This likely means the DB has been reset and you need to re-register.");
            else
                post.Poster = poster;

            if (topic == null && string.IsNullOrWhiteSpace(TopicName))
                ModelState.AddModelError("TopicName", "You must either pick a topic or create a new one.");
            else if (topic == null)
            {
                topic = new Topic() { TopicName = TopicName, Creator = poster };
                if (TryValidateModel(topic)) post.PostedIn = topic;
            }
            else
                post.PostedIn = topic;
            
            if (ModelState.IsValid)
            {
                post.Date = DateTime.Now;
                db.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Members = new SelectList(db.Users.OrderBy(m => m.UserName), "Id", "UserName");
            ViewBag.Topics = new SelectList(db.Topics.OrderBy(t => t.TopicName), "TopicID", "TopicName");

            return View(post);
        }
Esempio n. 2
0
        public ActionResult Create([Bind(Include = "TopicPostID,Body")] TopicPost post, int? Members, int? Topics, string TopicName)
        {
            Topic topic = db.Topics.Find(Topics);
            Member poster = db.Members.Find(Members);

            if (poster == null)
                ModelState.AddModelError("Poster", "You must choose a member to post as.");
            else
                post.Poster = poster;

            if (topic == null && string.IsNullOrWhiteSpace(TopicName))
                ModelState.AddModelError("TopicName", "You must either pick a topic or create a new one.");
            else if (topic == null)
            {
                topic = new Topic() { TopicName = TopicName, Creator = poster };
                if (TryValidateModel(topic)) post.PostedIn = topic;
            }
            else
                post.PostedIn = topic;
            
            if (ModelState.IsValid)
            {
                post.Date = DateTime.Now;
                db.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Members = new SelectList(db.Members.OrderBy(m => m.UserName), "MemberID", "UserName");
            ViewBag.Topics = new SelectList(db.Topics.OrderBy(t => t.TopicName), "TopicID", "TopicName");

            return View(post);
        }
Esempio n. 3
0
        public ActionResult Create([Bind(Include = "TopicPostID,Body")] TopicPost post, int Members, int? Topics, string TopicName)
        {
            if (ModelState.IsValid)
            {
                Topic topic = db.Topics.Find(Topics);
                Member poster = db.Members.Find(Members);

                if (topic == null)
                {
                    topic = new Topic() { TopicName = TopicName, Creator = poster };
                    db.Topics.Add(topic);
                }

                post.Date = DateTime.Now;
                post.Poster = poster;
                post.PostedIn = topic;
                db.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Members = new SelectList(db.Members.OrderBy(m => m.UserName), "MemberID", "UserName");
            ViewBag.Topics = new SelectList(db.Topics.OrderBy(t => t.TopicName), "TopicID", "TopicName");

            return View(post);
        }