// GET: Boardgames/Create public ActionResult Create() { CreateBoardgameViewModel createBoardgameViewModel = new CreateBoardgameViewModel(); UpdateCreateBoardgameViewModel(createBoardgameViewModel); return(View(createBoardgameViewModel)); }
public ActionResult Create(CreateBoardgameViewModel createBoardgameViewModel) { if (ModelState.IsValid) { AddBoardgame(createBoardgameViewModel); return(RedirectToAction("Index", "Home")); } UpdateCreateBoardgameViewModel(createBoardgameViewModel); return(View(createBoardgameViewModel)); }
private void UpdateCreateBoardgameViewModel(CreateBoardgameViewModel createBoardgameViewModel) { //Load Board Game Types var boardGameTypes = db.BoardGameTypes.ToList() .Select(x => new SelectListItem { Text = x.Name, Value = (x.Id).ToString() }) .OrderBy(x => x.Text); createBoardgameViewModel.AvailableBoardGameTypes = new SelectList(boardGameTypes, nameof(SelectListItem.Value), nameof(SelectListItem.Text)); }
private void AddBoardgame(CreateBoardgameViewModel viewModel) { Boardgame boardgame = new Boardgame(); MapperConfiguration config = new MapperConfiguration(cfg => cfg.CreateMap <CreateBoardgameViewModel, Boardgame>()); IMapper mapper = config.CreateMapper(); boardgame = mapper.Map <Boardgame>(viewModel); boardgame.CreatedBy = HttpContext.User.Identity.Name; boardgame.CreatedDate = DateTime.Now; db.Boardgames.Add(boardgame); db.SaveChanges(); }