示例#1
0
        public ActionResult Create(CommentCreateModel model)
        {
            if (ModelState.IsValid)
            {
                int userID = int.Parse(User.Identity.Name);
                Comment comment = new Comment();
                comment.Text = model.Text;
                comment.SiteUserId = userID;
                comment.UpdatedBy = userID;
                comment.UpdatedOn = DateTime.Now;
                comment.CreatedBy = userID;
                comment.CreatedOn = DateTime.Now;
                comment = Repository.Add(comment);

                Mapper.CreateMap<SiteUser, UserDetailsModel>();
                Mapper.CreateMap<Comment, CommentDetailsModel>();
                var details = Mapper.Map<Comment, CommentDetailsModel>(comment);

                return Json(new { appendto = "#comments", content = ControllerHelper.RenderPartialView(this, "Details", details) });
            }
            else
            {
                return Json(new { error = true, message = "The values entered are not valid" });
            }
        }
        public Comment Add(Comment item)
        {
            item = db.Comments.Add(item);
            db.SaveChanges();

            item.SiteUser = db.Users.Find(item.SiteUserId);

            return item;
        }
        public void Setup()
        {
            commentRepository = new Mock<ICommentRepository>();
            controller = new CommentController(commentRepository.Object);

            var mockContext = new Mock<ControllerContext>();
            mockContext.SetupGet(p => p.HttpContext.User.Identity.Name).Returns("1");
            mockContext.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);
            controller.ControllerContext = mockContext.Object;

            var mockHelper = new Moq.Mock<ControllerHelper>();
            mockHelper.Setup(x => x.RenderPartialView(It.IsAny<Controller>(), It.IsAny<string>(), It.IsAny<object>())).Returns("View Content");

            controller.ControllerHelper = mockHelper.Object;

            comment = new Comment();
            comment.CommentId = 1;
        }
 public Comment Update(Comment item)
 {
     db.Entry(item).State = EntityState.Modified;
     db.SaveChanges();
     return item;
 }