public ActionResult Create([Bind(Include = "PersonId,Name,DOB,Email,Phone")] Person person) { if (ModelState.IsValid) { db.People.Add(person); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(person)); }
public ActionResult Create([Bind(Include = "AddressId,HouseNo,Line1,Line2,CountryId,CityId,PersonId")] Address address) { //additional validation to check correct county is selected for selected city if (address.CountryId != db.Cities.Where(c => c.CityId == address.CityId).Select(c => c.Country.CountryId).FirstOrDefault()) { ModelState.AddModelError("CountryId", "Invalid Country Selected."); } if (ModelState.IsValid) { db.Addresses.Add(address); db.SaveChanges(); return(RedirectToAction("Details", "Person", new { id = address.PersonId })); } ViewBag.CityId = new SelectList(db.Cities, "CityId", "CityName", address.CityId); ViewBag.CountryId = new SelectList(db.Countries, "CountryId", "CountryName", address.CountryId); ViewBag.PersonId = new SelectList(db.People, "PersonId", "Name", address.PersonId); ViewBag.Person = db.People.Find(address.PersonId); return(View(address)); }
private void InitializeDataBase() { options = new DbContextOptionsBuilder <ProjContext>().UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; using (var context = new ProjContext(options)) { context.Atividade.Add(new Atividade { Descricao = "ativ 1", DataInicio = DateTime.ParseExact("04/01/2021", "d", CultureInfo.InvariantCulture), DataFim = DateTime.ParseExact("05/22/2021", "d", CultureInfo.InvariantCulture), Responsavel = new Responsavel { Id = 1, Nome = "Leo" } }); context.SaveChanges(); context.Atividade.Add(new Atividade { Descricao = "ativ 2", DataInicio = DateTime.ParseExact("04/01/2021", "d", CultureInfo.InvariantCulture), DataFim = DateTime.ParseExact("05/22/2021", "d", CultureInfo.InvariantCulture), Responsavel = context.Responsavel.FirstOrDefault(a => a.Id == 1) }); context.SaveChanges(); } }