public async Task <IActionResult> Create([Bind("Id,Description,ReleasedDate,Category,NeedIds, CatId")] DonationViewModel donationModel) { if (ModelState.IsValid) { // 1. Create a new var donation = new Donation { // 2. Set values Id = donationModel.Id, Description = donationModel.Description, ReleasedDate = donationModel.ReleasedDate, Category = donationModel.Category, Owner = _userManager.GetUserAsync(User).Result, CategorySelect = _context.Categories.Where(cat => cat.Id == donationModel.CatId).First() }; donation.DonationNeeds = new List <DonationNeed>(); if (donationModel.NeedIds != null) { List <Need> allNeeds = _context.Needs.Where(need => donationModel.NeedIds.Contains(need.Id)).ToList(); foreach (Need need in allNeeds) { donation.DonationNeeds.Add(new DonationNeed { Need = need, Donation = donation }); } _context.Add(donation); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { _context.Add(donation); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } return(View(donationModel)); }
public async Task <IActionResult> Create([Bind("FirstName,LastName,Location,Id,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount")] User user) { if (ModelState.IsValid) { _context.Add(user); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(user)); }
public async Task <IActionResult> Create([Bind("Id,Description,ReleaseDate,Category, CatId")] Need needModel) { if (ModelState.IsValid) { // 1. Create a new var need = new Need { // 2. Set values fr Id = needModel.Id, Description = needModel.Description, ReleaseDate = needModel.ReleaseDate, Category = needModel.Category, Owner = _userManager.GetUserAsync(User).Result, CategorySelect = _context.Categories.Where(cat => cat.Id == needModel.CatId).First() }; _context.Add(need); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(needModel)); }