コード例 #1
0
        public ActionResult Create(string name, string text, string category)
        {
            try
            {
                if((name == "") || (text == "")) throw new ArgumentException("Name/Text");

                Comment newComment = new Comment();
                newComment.Name = name;
                newComment.Text = text;
                newComment.Category = new Category();
                newComment.Category.Id = new Guid(category);

                this._rep.CreateComment(newComment);
                return RedirectToAction("Index");
            }
            catch
            {
                if (name == "") ModelState.AddModelError("Name", "Set Name");
                if (text == "") ModelState.AddModelError("Text", "Set Text");

                List<Category> result = this._rep.GetCategories().ToList();
                SelectList select = new SelectList(result, "Id", "Name");

                return View(select);
            }
        }
コード例 #2
0
        public void CreateComment(Comment comment)
        {
            Comments newComment = new Comments();
            newComment.Id = Guid.NewGuid();
            newComment.DateTime = DateTime.Now;
            newComment.Name = comment.Name;
            newComment.Text = comment.Text;

            Categories addedCat = (from x in _context.Categories
                                   where x.Id == comment.Category.Id
                                   select x).FirstOrDefault();

            newComment.Categories = addedCat;

            _context.AddToComments(newComment);
            _context.SaveChanges();
        }