public ActionResult View(int id)
        {
            var model = new ViewBoardModel();

            using (var db = new lookatmyskateboardEntities())
            {
                var board = db.Skateboards.Include(x => x.User).Include(x => x.Comments).First(x => x.id == id);
                model.Board = new BoardModel
                {
                    Id          = board.id,
                    Name        = board.name,
                    Description = board.description,
                    ImageUrl    = board.imageUrl,
                    UploadedBy  = board.User.username
                };
                model.Comments = board.Comments.Select(x => new CommentModel {
                    User = x.User.username, Text = x.Text
                }).ToList();
                model.NewComment = new CommentModel
                {
                    Text   = string.Empty,
                    UserId = Session["User"] != null ? (Session["User"] as UserAccount).UserId : new int?(),
                    User   = Session["User"] != null ? (Session["User"] as UserAccount).Username : string.Empty
                };
                if (Session["User"] != null)
                {
                    model.Liked = board.Likes.Contains(db.Users.Find((Session["User"] as UserAccount).UserId));
                }
            }

            return(View(model));
        }
        public ActionResult AddComment(ViewBoardModel model)
        {
            var cs = ConfigurationManager.ConnectionStrings["lookatmyskateboardConnection"].ConnectionString;

            using (var cn = new SqlConnection(cs))
            {
                cn.Open();
                var sql = "INSERT INTO Comment (skateboardid, userid, text) VALUES (" + model.Board.Id + ", " +
                          model.NewComment.UserId.Value + ", '" + model.NewComment.Text.Replace("'", "''") + "');";

                using (var cmd = new SqlCommand(sql, cn))
                {
                    cmd.ExecuteNonQuery();
                }
            }

            return(RedirectToAction("View", new { id = model.Board.Id }));
        }
示例#3
0
        // GET: Board
        public async Task <IActionResult> Index()
        {
            var model = new ViewBoardModel();

            model.Players    = _context.Players.ToList();
            model.PlayerSums = new Dictionary <Player, decimal>();
            foreach (var player in model.Players)
            {
                model.PlayerSums.Add(player, _context.Board.Where(be => be.Player.Id == player.Id).Sum(be => be.Difference));
            }
            model.Board = new Dictionary <GamePlayed, IEnumerable <BoardEntry> >();
            foreach (var gamePlayed in _context.GamesPlayed.Include(gp => gp.Game).OrderByDescending(gp => gp.DateTime))
            {
                model.Board.Add(gamePlayed, _context.Board.Include(be => be.Player).Where(entry => entry.Game.Id == gamePlayed.Id));
            }

            return(View(model));
        }