public ActionResult Create(CreateRegattaViewModel createRegattaViewModel) { if (ModelState.IsValid) { AddressContact addressContact = CreateAddressContact(createRegattaViewModel); ContactPerson contactPerson = CreateContactPerson(createRegattaViewModel); Mapper.Initialize(cfg => cfg.CreateMap <PocoClasses.Regattas.Regatta, Regatta>()); Regatta regatta = Mapper.Map <Regatta>(createRegattaViewModel.Regatta); regatta.AddressContactId = addressContact.Id; regatta.ContactPersonsId = contactPerson.Id; regatta.HostingClubId = FindClubId(); using (var context = new RegattaContext()) { var service = new RegattaService(context); service.Add(regatta); } ViewBag.AddressContactId = new SelectList(new AddressContactContext().AddressContacts, "Id", "NextOfKin", regatta.AddressContactId); // ViewBag.AddressContactId = new SelectList(new AddressContactService(new AddressContactContext()).GetAll(),"Id","NextOfKin",regatta.AddressContactId); // ViewBag.HostingClubId = new SelectList(new ClubService(new ClubContext()).GetAll(), "Id", "Name", regatta.HostingClubId); // ViewBag.ContactPersonsId = new SelectList(new ContactPersonService(new ContactPersonContext()).GetAll(), "Id", "Email", regatta.ContactPersonsId); return(RedirectToAction("Index")); } return(View(createRegattaViewModel)); }
public async Task <IActionResult> Create([Bind("Title,HostingClubId,Description,StartTime,EndTime,Address,Id")] CreateRegattaViewModel regatta) { if (ModelState.IsValid) { var _currentUser = GetCurrentUser(); var hostClub = await _context.Clubs.SingleOrDefaultAsync(c => c.Id.Equals(regatta.HostingClubId)); var newRegatta = new Regatta { Id = Guid.NewGuid(), CreatedBy = _currentUser, HostingClub = hostClub, Title = regatta.Title, Address = regatta.Address, Description = regatta.Description, StartTime = regatta.StartTime, EndTime = regatta.EndTime }; _context.Add(newRegatta); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(regatta)); }
// GET: Regattas/Create public async Task <IActionResult> Create() { var userId = GetCurrentUser().Id; var clubsSelectListItems = await _context.Clubs .Include(c => c.Owner) .Where(c => c.Owner.Id == userId) .Select(club => new SelectListItem { Value = club.Id.ToString(), Text = club.Name }) .ToListAsync(); if (clubsSelectListItems.Count == 0) { return(RedirectToAction("Create", "Clubs")); } clubsSelectListItems.Insert(0, new SelectListItem { Value = "", Text = "Välj värdklubb" }); var createRegattaViewModel = new CreateRegattaViewModel { HostingClubs = new SelectList(clubsSelectListItems, "Value", "Text"), StartTime = DateTime.Today, EndTime = DateTime.Today.AddDays(1) }; return(View(createRegattaViewModel)); }
private ContactPerson CreateContactPerson(CreateRegattaViewModel createRegattaViewModel) { Mapper.Initialize(cfg => cfg.CreateMap <PocoClasses.ContactPerson, ContactPerson>()); ContactPerson contactPerson = Mapper.Map <ContactPerson>(createRegattaViewModel.ContactPerson); using (var context = new ContactPersonContext()) { var service = new ContactPersonService(context); service.Add(contactPerson); } return(contactPerson); }
private AddressContact CreateAddressContact(CreateRegattaViewModel createRegattaViewModel) { Mapper.Initialize(cfg => cfg.CreateMap <PocoClasses.AddressContacts.AddressContact, AddressContact>()); AddressContact addressContact = Mapper.Map <AddressContact>(createRegattaViewModel.AddressContact); using (var context = new AddressContactContext()) { var service = new AddressContactService(context); service.Add(addressContact); } return(addressContact); }