public async Task <IActionResult> Create([Bind("Title, Content, UserId, CityId, Date")] Proposition proposition) { var cities = await _context.Cities.ToListAsync(); ViewData["Cities"] = cities; var user = GetUser(); try { if (ModelState.IsValid) { proposition.UserId = user.Id; proposition.Date = DateTime.Now; _context.Add(proposition); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Proposition")); } } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(proposition)); }
public async Task <IActionResult> CreateComment(int propositionId, [Bind("Content, PublicationDate, UserId, PropositionId")] Comment comment) { var user = GetUser(); try { if (ModelState.IsValid) { comment.PublicationDate = DateTime.Now; comment.UserId = user.Id; comment.PropositionId = propositionId; _context.Add(comment); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Proposition", new { id = propositionId })); } } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(comment)); }
public async Task <IActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { User user = await _context.Users.FirstOrDefaultAsync(u => u.Email == model.Email); if (user == null) { user = new User { Name = model.Name, Email = model.Email, Password = model.Password }; Role userRole = _context.Roles.FirstOrDefault(r => r.Name == "user"); if (userRole != null) { user.Role = userRole; } _context.Users.Add(user); await _context.SaveChangesAsync(); await Authenticate(user); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("", "Некорректные логин и(или) пароль"); } } return(View(model)); }