예제 #1
0
        public async Task <IActionResult> Chat(int id)
        {
            var currentUser = await _userManager.GetUserAsync(User);

            ViewBag.CurrentUserName = currentUser.UserName;
            var messages = await _appContext.Messages.ToListAsync();

            HomeController        home  = new HomeController(_appContext);
            ChannelsChatViewModel model = new ChannelsChatViewModel();

            model.ChannelId = id;
            model.Messages  = _appContext.Messages.Include(u => u.User).Where(m => m.Channel.Id == id).ToList();
            model.Channel   = _appContext.Channels.FirstOrDefault(c => c.Id == id);
            BaseViewModel vm = home.MakeSideList(model);

            return(View(vm));
        }
예제 #2
0
        public async Task <IActionResult> PostMessage(ChannelsChatViewModel model, int id)
        {
            if (ModelState.IsValid)
            {
                ///*message.User.UserName*/ = User.Identity.Name;
                var sender = await _userManager.GetUserAsync(User);

                //message.User.Id = sender.Id;
                await _appContext.Messages.AddAsync(new Message()
                {
                    ChatMessage = model.Message,
                    User        = sender,
                    Channel     = _appContext.Channels.FirstOrDefault(c => c.Id == id)
                });

                await _appContext.SaveChangesAsync();
            }
            return(RedirectToAction("Chat", new { id = id }



                                    ));
        }